Android中碎片的使用方法詳解
Fragment的使用
其實(shí)碎片很簡(jiǎn)單,但是網(wǎng)上胡亂充數(shù)的博文太多了,以至于我們有時(shí)候覺(jué)得比較亂,今天就來(lái)簡(jiǎn)單講解一下碎片的使用.
碎片的使用分為兩種,靜態(tài)添加碎片和動(dòng)態(tài)添加碎片,我們就先來(lái)看一下靜態(tài)添加碎片如何實(shí)現(xiàn).
靜態(tài)添加碎片
首先,先建兩個(gè)Layout文件,這就是碎片的布局文件,大家可能也發(fā)現(xiàn)了,Android Studio里面可以直接快速建立碎片,就像Activity一樣,但是這樣會(huì)生成很多沒(méi)用的代碼,所以我們還是選擇自己創(chuàng)建碎片布局.
兩個(gè)布局都很簡(jiǎn)單,里面只有一個(gè)居中顯示的TextView,下面貼一下代碼.
第一個(gè)布局文件:fragment_first.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" android:background="@android:color/holo_blue_light" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="This is the first fragment!"/> </LinearLayout>
第二個(gè)布局文件fragment_second.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" android:background="@android:color/holo_green_light" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="This is the second fragment!"/> </LinearLayout>
現(xiàn)在布局文件建完了,我們?cè)摻⑺麄儗?duì)應(yīng)的Fragment了,也就是后臺(tái)代碼了。新建兩個(gè)類(lèi),分別叫FirstFragment和SecondFragment,都繼承于Fragment,需要注意一點(diǎn),我們教程里面所使用的Fragment全都是android.support.v4.app.Fragment這個(gè)包下的,這樣更有利于程序的兼容性.
貼一下兩個(gè)類(lèi)的代碼,也很簡(jiǎn)單,只是重寫(xiě)了onCreateView方法來(lái)加載不同的布局文件.
public class FirstFragment extends Fragment { private View view;//得到碎片對(duì)應(yīng)的布局文件,方便后續(xù)使用 //記住一定要重寫(xiě)onCreateView方法 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_first, container, false);//得到對(duì)應(yīng)的布局文件 return view; } } public class SecondFragment extends Fragment { private View view;//得到碎片對(duì)應(yīng)的布局文件,方便后續(xù)使用 //記住一定要重寫(xiě)onCreateView方法 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_second, container, false);//得到對(duì)應(yīng)的布局文件 return view; } }
好,基本的工作我們做完了,現(xiàn)在我們用兩個(gè)Activity展示如何靜態(tài)添加碎片和動(dòng)態(tài)添加碎片.
靜態(tài)添加控件的話,需要使用fragment控件,指定其名稱(chēng)是你剛才創(chuàng)建的Fragment就可以,讓我們來(lái)看一下.
先貼一下第一個(gè)Activity的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="me.worldskills.android.testfragment.MainActivity"> <fragment android:id="@+id/main_firstfragment" android:name="me.worldskills.android.testfragment.FirstFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></fragment> <fragment android:id="@+id/main_secondfragment" android:name="me.worldskills.android.testfragment.SecondFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></fragment> <Button android:id="@+id/main_btnGoNext" android:layout_width="match_parent" android:layout_height="50dp" android:text="Go to next page" android:textAllCaps="false"/> </LinearLayout>
其中那個(gè)按鈕是用來(lái)跳轉(zhuǎn)到下一個(gè)界面的,也就是動(dòng)態(tài)添加碎片案例的Activity,在這里可以忽略.
這里我們看見(jiàn)了,兩個(gè)fragment分別指定name為FirstFragment和SecondFragment,也就是你剛才創(chuàng)建的兩個(gè)Fragment,一定要記得加上包名.對(duì)了,還有一個(gè)問(wèn)題,就是這樣的話是沒(méi)有預(yù)覽的,如果想要預(yù)覽,需要在fragment標(biāo)簽中加上一句代碼:
Tools:layout="@layout/布局文件名稱(chēng)"
.
好了,靜態(tài)添加碎片就完成了,什么?就這么簡(jiǎn)單,對(duì)啊...就這么簡(jiǎn)單.
動(dòng)態(tài)添加碎片
動(dòng)態(tài)添加碎片我們就不需要用fragment控件了,而是需要用個(gè)FrameLayout控件,這是為什么呢,首先我們都知道FrameLayout中的控件,都是從左上角開(kāi)始顯示,不用進(jìn)行位置控制,動(dòng)態(tài)添加碎片其實(shí)就是向容器里面動(dòng)態(tài)添加碎片,而fragment控件只能用來(lái)靜態(tài)綁定一個(gè)碎片.
先貼一下第二個(gè)Activity的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_second" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/second_btnLoadFirst" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Load First!" android:textAllCaps="false"/> <Button android:id="@+id/second_btnLoadSecond" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Load Second!" android:textAllCaps="false"/> <FrameLayout android:id="@+id/second_fl" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout>
上面的兩個(gè)按鈕用來(lái)加載不同的碎片,而下面的FrameLayout就是碎片顯示的容器.
廢話不多說(shuō),貼代碼:
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //點(diǎn)擊第一個(gè)按鈕的時(shí)候加載第一個(gè)碎片 findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.second_fl, new FirstFragment()); transaction.commit(); } }); //點(diǎn)擊第二個(gè)按鈕的時(shí)候加載第二個(gè)碎片 findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//簡(jiǎn)寫(xiě) } }); } }
getSupportFragmentManager方法用來(lái)獲得一個(gè)碎片管理器對(duì)象(使用這個(gè)方法的時(shí)候注意是android.support.v4.app包下的哦),然后通過(guò)這個(gè)方法開(kāi)始一個(gè)碎片事物對(duì)象,這個(gè)對(duì)象比較關(guān)鍵,可以用來(lái)動(dòng)態(tài)添加碎片,調(diào)用它的replace方法,會(huì)把指定容器里面的其他控件全部清除掉,然后添加新的碎片進(jìn)去.在這里就是先把R.id.second_f1里面的控件清空,然后添加傳入一個(gè)FirstFragment進(jìn)去.
替換完之后一定要記得調(diào)用commit方法提交,要不然你的所有操作都不會(huì)生效,切記.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ListView 條目多樣式展示實(shí)例詳解
這篇文章主要介紹了Android ListView 條目多樣式展示的相關(guān)資料,需要的朋友可以參考下2017-04-04Android實(shí)現(xiàn)簡(jiǎn)單斷點(diǎn)續(xù)傳和下載到本地功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單斷點(diǎn)續(xù)傳和下載到本地功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android開(kāi)發(fā)筆記之:如何屏蔽Button setClickable與setEnabled
本篇文章是對(duì)在Android中,如何屏蔽Button setClickable與setEnabled的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁(yè)滑動(dòng)效果
這篇文章主要介紹了AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁(yè)滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10android 開(kāi)發(fā)教程之日歷項(xiàng)目實(shí)踐(三)
決定開(kāi)始學(xué)習(xí) Android 平臺(tái)下的軟件開(kāi)發(fā),以日歷作為實(shí)踐項(xiàng)目,進(jìn)行一周后,基本完成,有需要的朋友可以參考下2013-01-01Android編程實(shí)現(xiàn)簡(jiǎn)易彈幕效果示例【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡(jiǎn)易彈幕效果,涉及Android動(dòng)畫(huà)及視圖動(dòng)態(tài)操作相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01Android 帶進(jìn)度條的WebView 示例代碼
本文主要介紹Android WebView,這里提供實(shí)例代碼,和效果圖供大家參考,希望能幫助有需要的小伙伴2016-07-07Android View事件分發(fā)和消費(fèi)源碼簡(jiǎn)單理解
這篇文章主要介紹了Android View事件分發(fā)和消費(fèi)源碼簡(jiǎn)單理解的相關(guān)資料,需要的朋友可以參考下2017-07-07