Android 第十七課 碎片的簡單用法及動態添加碎片

Fragment(碎片)是一種可以嵌入在活動當中的UI片段,它可以讓程序更加合理和充分的利用大屏幕的空間。碎片和活動太像了,同樣都包含布局,都有自己的聲明周期,可以將碎片理解為一種迷你型的活動。

新建FragmentTest項目。假設項目已經建立完成。

新建一個左側布局left_fragment.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:text="Button"/>
</LinearLayout>

只是放置了一個按鈕,并讓它水平居中顯示,然后:
新建右側碎片布局right_fragment.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:background="#00ff00"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/>
</LinearLayout>

我們只是將這個布局的背景設置成了綠色,并放置了一個TextView用于顯示一段文本。

接著新建一個LeftFragment類,這個類繼承自Fragment,這里有兩個不同包下的Fragment供你選擇,我們要選擇support-v4庫中的Fragment,因為它可以讓碎片在所有Android 版本中保持功能的一致性。另外,我們不需要在build.gradle文件中添加support-v4庫的依賴,因為build.gradle文件中已經添加了appcompat-v7庫的依賴,而這個庫,會將support-v4庫也一并引入進來。

現在編寫LeftFragment中的代碼,如下所示:

package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class LeftFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.left_fragment,container,false);return  view ;}
}

這里僅僅重寫了Fragment的onCreata()方法,然后在這個方法中通過LayoutInflater的inflate()方法將剛才定義的left_fragment布局動態加載進來,整個方法就簡單明了。

我們再新建一個RightFragment,代碼如下:

package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class RightFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.right_fragment,container,false);return  view ;}}

基本山代碼都是相同的,接下里修改acitivity_main.xml中的代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/left_fragment"android:name ="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><fragmentandroid:id="@+id/right_fragment"android:name ="com.example.fragmenttest.RightFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout>

我們使用了<fragment>標簽在布局中添加碎片,其中指定的大多數屬性都是比較熟悉的,只不過通過android:name屬性來顯式指明要添加地碎片類名,注意一定要將類的包名也加上。

我們可以運行一下程序:


兩個碎片平分了整個活動地布局。



2、動態添加碎片

碎片地真正地強大之處在于,它可以在程序運行時動態地添加到活動當中,根據具體情況來添加碎片,你就可以將程序界面定制的更加多樣化。

我們接著上一節的內容,新建another_right_fragment.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:background="#ffff00"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is another right fragment"/></LinearLayout>

這個布局文件和right_fragment.xml中的代碼基本相同,只是將背景色變成了黃色。

新建AnotherRightFragment作為另一個右側碎片,代碼如下:


package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class AnotherRightFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.another_right_fragment,container,false);return  view ;}
}
代碼同樣簡單。在onCreateView()方法中加載了剛剛創建的another_right_fragment布局。這樣我們就準備好了另一個碎片,接下來,看一下如何動態將它添加到活動當中,修改activity_main.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/left_fragment"android:name ="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><FrameLayoutandroid:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"></FrameLayout></LinearLayout>

現在將右側碎片替換成一個Fragment中,Fragment是Android中最簡單的一種布局,所有控件默認都會擺放在布局的左上角。

由于這里僅僅需要在布局里放入一個碎片,不需要任何定位,非常適合使用FragmentLayout。

我們在代碼中向Fragment中添加內容,從而實現動態添加碎片的功能。修改MainActivity中的代碼,如下:

package com.example.fragmenttest;import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Button button =(Button)findViewById(R.id.button);button.setOnClickListener(this);replaceFragment(new RightFragment());} @Overridepublic void onClick(View view) {switch (view.getId()){case R.id.button:replaceFragment(new AnotherRightFragment());break;default:break;}}private void replaceFragment(android.support.v4.app.Fragment fragment){FragmentManager fragmentManager = getSupportFragmentManager();android.support.v4.app.FragmentTransaction tranction = fragmentManager.beginTransaction();tranction.replace(R.id.right_layout,fragment);tranction.commit();}

}

首先我們給左側碎片中的按鈕注冊了一個點擊事件,然后調用replaceFragment()方法動態的添加Fragment這個碎片。當點擊按鈕時,又會調用replaceFragment()方法將右側碎片替換成AnotherRightFragment.結合replaceFramment()方法中的代碼可以看出,動態添加碎片主要分為5步。

1、創建待添加的碎片實例

2、創建FragmentManager,在活動中可以直接通過調用getSupportFragmentManager()方法得到。

3、開啟一個事務,通過調用beginTransaction方法開啟。

4、向容器內添加或替換碎片,一般使用replace()方法實現,需要傳入容器的id和待添加的碎片實例。

5、提交事務,調用commit()方法來完成。

運行一下程序:點擊按鈕可以看到效果:







本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274098.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274098.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274098.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在Linux下禁用鍵盤、鼠標、觸摸板(筆記本)等輸入設備

在Linux系統下禁用鍵盤、觸摸板、鼠標等輸入設備&#xff0c;可以通過xinput命令來實現&#xff1a;主要涉及&#xff1a;#xinput list#xinput list-props list-number#xinput set-prop list-number func-number 1/0具體操作如下&#xff1a;step1&#xff1a;查看系統中有那些…

委托又給我惹麻煩了————記委托鏈的取消注冊、獲取返回值

今天改bug碰到了一個問題&#xff0c;有多個方法注冊到了一個事件里去&#xff0c;而這些方法本身又有點兒互斥&#xff0c;因而造成了bug&#xff0c;哥調試半天才發現&#xff0c;郁悶至極&#xff0c;遂復習了以前的知識并進行適當延伸&#xff0c;再將成果記錄及分享之&…

Python第一課

對python仰慕已久&#xff0c;今日下定決心學習。可能我是一時頭腦發熱&#xff0c;但我還是愿意堅持。 先了解一下&#xff1a;命令行模式和Python交互模式 在Windows開始菜單選擇“命令提示符”&#xff0c;就進入到命令行模式&#xff0c;它的提示符類似C:\>&#xff1a;…

C++模板專門化與重載

最近在復習C有關知識&#xff0c;又重新看<<Effective C>>&#xff0c;收獲頗豐。原來以前看這邊書&#xff0c;好多地方都是淺嘗輒止。<<Effective C>>條款25&#xff1a;考慮寫出一個不拋出異常的swap函數&#xff0c;涉及到C模板專門化(Templates S…

Android 第十八課 強大的滾動控件 RecyclerView

步驟&#xff1a; 一、添加依賴庫compilecom.android.support:recyclerview-v7:26.1.0 二、在activity_mian.xml中&#xff0c;添加RecyclerView控件&#xff0c;并占據整個頁面。 三、把你要在RecyclerView中展示的內容&#xff0c;設置成一個實體類Fruit&#xff0c;接著為Re…

通過rtcwake命令設置系統S3(休眠到內存)/S4(掛起到硬盤)一段時間后自動喚醒

rtcwake -m disk -s 60 //S4&#xff08;掛起&#xff09;60秒后自動喚醒 rtcwake -m mem -s 60 //S3(休眠&#xff09;60秒后自動喚醒

電商首頁設計的時候,就應該考慮這個

如果有目的去找某一類商品的人幾乎都會從導航或搜索進去了&#xff0c;看首頁的一般是屬于那些還沒想好要買什么東西的人&#xff0c;這些人一般都是漫無目的的瞎逛&#xff0c;看在首頁有沒有特價的或便宜的東西被撿到。 轉載于:https://www.cnblogs.com/wangzong/p/3256555.h…

JavaScript 第一課 JavaScript簡史

1、JavaScript的起源 Java在理論上可以部署在任何環境下&#xff0c;但是JavaScript卻更傾向于只應用在Web瀏覽器。JavaScript是一種腳本語言&#xff0c;通常只能通過Web瀏覽器去完成一些操作而不能像普通意義上的程序那樣獨立運行。因為需要Web瀏覽器進行解釋和執行&#xff…

Linux下的屏保設置 xset s 與 xset dpms

Linux下的屏保設置 xset s 與 xset dpmshttp://bbs.chinaunix.net/archiver/?tid-2112889.html用xset q 可以查看當前屏保的設置情況&#xff0c;黑屏方式的屏保有兩種狀態&#xff1a;1. xset 的s參數后面可接兩個數字參數&#xff0c;前一個即是進入屏保的秒數&#xff0…

ios即時通訊客戶端開發之-mac上安裝MySQL

一、安裝 到MySQL官網上http://dev.mysql.com/downloads/mysql/&#xff0c;下載mysql可安裝dmg版本 比如&#xff1a;Mac OS X ver. 10.7 (x86, 64-bit), DMG Archive 下載完的文件為&#xff1a;mysql-5.6.10-osx10.7-x86_64.dmg 1.點擊&#xff0c;安裝包里的 2.點擊安裝 安…

Android 第十九課 大喇叭--廣播機制----動態注冊監聽網絡變化與靜態注冊實現開機啟動

為了便于進行 系統級別的消息通知&#xff0c;Android引入了一套廣播消息機制。 1、廣播機制簡介&#xff1a;因為Android中的每個應用程序都可以對自己感興趣的廣播盡心注冊&#xff0c;這樣程序只會接收自己所關心的廣播內容&#xff0c;這些廣播來自于系統的&#xff0c;也可…

dbus 和 policykit 實例篇(python)

dbus 和 policykit 實例篇&#xff08;python&#xff09; 使用policykit 的程序一般都有一個dbus daemon程序來完成相關操作&#xff0c;這個dbus daemon 會在系統注冊一個system bus 服務名&#xff0c;用于響應要求root privileged的操作&#xff0c;當dbus請求到達時會先驗…

一個實際的sonar代碼檢查的配置文件

國內私募機構九鼎控股打造APP&#xff0c;來就送 20元現金領取地址&#xff1a;http://jdb.jiudingcapital.com/phone.html內部邀請碼&#xff1a;C8E245J &#xff08;不寫邀請碼&#xff0c;沒有現金送&#xff09;國內私募機構九鼎控股打造&#xff0c;九鼎投資是在全國股份…

JavaScript 第二課 JavaScript語法

本章內容&#xff1a;語句變量和數組操作符條件語句和循環語句函數與對象 ------------------------------------------------------------- 準備&#xff1a; 編寫JavaScript腳本只需要一個普通地文本編輯器和一個Web瀏覽器就足啦。 用JavaScript編寫的代碼必須通過HTML/XHTML…

和菜鳥一起學linux之DBUS基礎學習記錄

轉自&#xff1a;http://blog.csdn.net/eastmoon502136/article/details/10044993 D-Bus三層架構 D-Bus是一個為應用程序間通信的消息總線系統, 用于進程之間的通信。它是個3層架構的IPC 系統&#xff0c;包括&#xff1a; 1、函數庫libdbus &#xff0c;用于兩個應用程序互…

Android 第二十課 廣播機制(大喇叭)----發送自定義廣播(包括發送標準廣播和發送有序廣播)

廣播分為兩種類型&#xff1a;標準廣播和有序廣播 我們來看一下具體這兩者的具體區別&#xff1a; 1、發送標準廣播 我們需要先定義一個廣播接收器來準備接收此廣播才行&#xff0c;否則也是白發。 新建一個MyBroadcastReceiver,代碼如下&#xff1a; package com.example.broa…

八大排序算法

概述 排序有內部排序和外部排序&#xff0c;內部排序是數據記錄在內存中進行排序&#xff0c;而外部排序是因排序的數據很大&#xff0c;一次不能容納全部的排序記錄&#xff0c;在排序過程中需要訪問外存。 我們這里說說八大排序就是內部排序。 當n較大&#xff0c;則應采用…

需求?

1 需求怎樣描述清楚&#xff1f; 利用用例技術&#xff0c;一般這里指的是系統用例&#xff1b;包括以下幾個內容&#xff1a; 用例視圖 系統的功能描述&#xff1b; 用例規約 規定了用戶和系統的交互過程&#xff1b;用戶如何使用系統&#xff1b;用戶如何交互&#xff0c;以及…

Android 第二十一課 RecyclerView簡單的應用之編寫“精美”的聊天頁面

1、由于我們會使用到RecyclerView&#xff0c;因此首先需要在app/build.gradle當中添加依賴庫。如下&#xff1a; apply plugin: com.android.application .... dependencies {....compile com.android.support:recyclerview-v7:26.1.0 } 2、然后開始編寫主頁面&#xff0c;修該…

VS 2008 生成操作中各個選項的差別

近日&#xff0c;在編譯C#項目時經常發現有些時候明明代碼沒錯&#xff0c;但就是編譯不過&#xff0c;只有選擇重新編譯或者清理再編譯才會不出錯&#xff0c;本著求學的態度&#xff0c;搜羅了下VS2008IDE中生成操作的種類以及差別&#xff0c;整理如下&#xff1a;內容(Cont…