Android 嵌套Fragment的使用實(shí)例代碼
前言
之前的文章有介紹ActivityGroup,不少人問(wèn)嵌套使用的問(wèn)題,同樣的需求在Fragment中也存在,幸好在最新的Android support 包已經(jīng)支持這一特性!這里就跳過(guò)Fragment的介紹,需要注意的是TabActivity已經(jīng)被標(biāo)記為棄用(deprecated)。
正文
一、準(zhǔn)備
關(guān)于最新的Android兼容包的介紹,參見官網(wǎng)??梢栽赼ndroid sdk目錄下extras/android/support/v13/android-support-v13.jar找到最新版,注意是伴隨著Android 4.2一起更新的。
關(guān)于嵌套Fragment的介紹,參照官網(wǎng)。
二、截圖

三、代碼
FragmentNestActivity.java
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 嵌套Fragment使用
*
* @author 農(nóng)民伯伯
* @see http://www.cnblogs.com/over140/archive/2013/01/02/2842227.html
*
*/
public class FragmentNestActivity extends FragmentActivity implements OnClickListener {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.nested_fragments);
findViewById(R.id.btnModule1).setOnClickListener(this);
findViewById(R.id.btnModule2).setOnClickListener(this);
findViewById(R.id.btnModule3).setOnClickListener(this);
findViewById(R.id.btnModule1).performClick();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnModule1:
addFragmentToStack(FragmentParent.newInstance(0));
break;
case R.id.btnModule2:
addFragmentToStack(FragmentParent.newInstance(1));
break;
case R.id.btnModule3:
addFragmentToStack(FragmentParent.newInstance(2));
break;
}
}
private void addFragmentToStack(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_in_left);
ft.replace(R.id.fragment_container, fragment);
ft.commit();
}
/** 嵌套Fragment */
public final static class FragmentParent extends Fragment {
public static final FragmentParent newInstance(int position) {
FragmentParent f = new FragmentParent();
Bundle args = new Bundle(2);
args.putInt("position", position);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View convertView = inflater.inflate(R.layout.viewpager_fragments, container, false);
ViewPager pager = (ViewPager) convertView.findViewById(R.id.pager);
final int parent_position = getArguments().getInt("position");
//注意這里的代碼
pager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(final int position) {
return new Fragment() {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView convertView = new TextView(getActivity());
convertView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
convertView.setGravity(Gravity.CENTER);
convertView.setTextSize(30);
convertView.setTextColor(Color.BLACK);
convertView.setText("Page " + position);
return convertView;
}
};
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + parent_position + " - " + position;
}
});
return convertView;
}
}
}
代碼說(shuō)明:
這里最關(guān)鍵的是方法getChildFragmentManager的支持。這里也演示了Fragment作為嵌套內(nèi)部類的使用方法。
nested_fragments.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:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:background="#F7F5DE" >
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/black"
android:orientation="horizontal" >
<ImageView
android:id="@+id/btnModule1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:src="@android:drawable/ic_dialog_dialer" />
<ImageView
android:id="@+id/btnModule2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:src="@android:drawable/ic_dialog_info" />
<ImageView
android:id="@+id/btnModule3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:src="@android:drawable/ic_dialog_alert" />
</LinearLayout>
</LinearLayout>
viewpager_fragments.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.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</LinearLayout>
代碼說(shuō)明:
注意!實(shí)踐發(fā)現(xiàn)ViewPager并不能作為頂層容器,否則會(huì)報(bào)錯(cuò)。
四、說(shuō)明
這是一個(gè)典型的嵌套Fragment的例子,最外層使用FrameLayout來(lái)實(shí)現(xiàn)幾大模塊的切換,內(nèi)部使用ViewPager實(shí)現(xiàn)子模塊的切換,非常實(shí)用。
結(jié)束
考慮把Support Package, revision 11 更新翻譯一下,強(qiáng)烈建議大家升級(jí)到最新的兼容包。
相關(guān)文章
Android基礎(chǔ)開發(fā)之手勢(shì)識(shí)別
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)開發(fā)之手勢(shì)識(shí)別的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06
Android?Jetpack組件ViewModel基本用法詳解
這篇文章主要為大家介紹了Android?Jetpack組件ViewModel基本用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android?Flutter中異常處理的方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Android?Flutter中異常處理的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
android 9.0 launcher3 去掉抽屜式顯示所有 app(代碼詳解)
本文通過(guò)實(shí)例代碼給大家介紹了android 9.0 Launcher3 去掉抽屜式,顯示所有 app,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android實(shí)現(xiàn)無(wú)標(biāo)題欄全屏的方法
這篇文章主要介紹了Android實(shí)現(xiàn)無(wú)標(biāo)題欄全屏的三種方法,感興趣的小伙伴們可以參考一下2016-07-07
Android中CheckBox復(fù)選框控件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android中CheckBox復(fù)選框控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)平滑翻動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)平滑翻動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04

