Android 嵌套Fragment的使用实例代码

 更新时间:2016年07月23日 09:56:03   投稿:lqh  
本文主要介绍Android Fragment,在这里提供了实例代码跟效果图,希望能帮助有需要的小伙伴

前言

  之前的文章有介绍ActivityGroup,不少人问嵌套使用的问题,同样的需求在Fragment中也存在,幸好在最新的Android support 包已经支持这一特性!这里就跳过Fragment的介绍,需要注意的是TabActivity已经被标记为弃用(deprecated)。

正文

 一、准备

  关于最新的Android兼容包的介绍,参见官网。可以在android sdk目录下extras/android/support/v13/android-support-v13.jar找到最新版,注意是伴随着Android 4.2一起更新的。

  关于嵌套Fragment的介绍,参照官网。

二、截图

 三、代码

  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 农民伯伯
 * @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;
    }
  }
}

代码说明:

    这里最关键的是方法getChildFragmentManager的支持。这里也演示了Fragment作为嵌套内部类的使用方法。

 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>

代码说明:

   注意!实践发现ViewPager并不能作为顶层容器,否则会报错。

 四、说明

  这是一个典型的嵌套Fragment的例子,最外层使用FrameLayout来实现几大模块的切换,内部使用ViewPager实现子模块的切换,非常实用。

结束

 考虑把Support Package, revision 11 更新翻译一下,强烈建议大家升级到最新的兼容包。

相关文章

  • Android自定义TabLayout效果

    Android自定义TabLayout效果

    这篇文章主要为大家详细介绍了Android自定义TabLayout效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android TextView Marquee的应用实例详解

    Android TextView Marquee的应用实例详解

    这篇文章主要介绍了Android TextView Marquee的应用实例详解的相关资料,这里说明使用方法及简单实例和注意实现,需要的朋友可以参考下
    2017-08-08
  • Android高仿微信对话列表滑动删除效果

    Android高仿微信对话列表滑动删除效果

    这篇文章主要为大家详细介绍了Android高仿微信对话列表滑动删除效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Flutter Android端启动白屏问题的解决

    Flutter Android端启动白屏问题的解决

    Flutter 应用在 Android 端上启动时会有一段很明显的白屏现象,白屏的时长由设备的性能决定,设备性能越差,白屏时间越长。这篇文章主要介绍了Flutter Android端启动白屏问题的解决。感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android实现简易的柱状图和曲线图表实例代码

    Android实现简易的柱状图和曲线图表实例代码

    柱状图是统计图表中经常用到的一种图表,比如降雨量之类的统计展示。这篇文章主要给大家介绍了关于利用Android如何实现简易的柱状图和曲线图表的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2017-12-12
  • Android使用glide加载gif动画设置播放次数

    Android使用glide加载gif动画设置播放次数

    这篇文章主要为大家详细介绍了Android使用glide加载gif动画设置播放次数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android开发之项目模块化实践教程

    Android开发之项目模块化实践教程

    这篇文章主要给大家介绍了关于Android开发之项目模块化的相关资料,文中通过示例代码给各位Android开发者们介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习下吧。
    2017-09-09
  • Android之获取手机内部及sdcard存储空间的方法

    Android之获取手机内部及sdcard存储空间的方法

    今天小编就为大家分享一篇Android之获取手机内部及sdcard存储空间的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Flutter交互并使用小工具管理其状态widget的state详解

    Flutter交互并使用小工具管理其状态widget的state详解

    这篇文章主要为大家介绍了Flutter交互并使用小工具管理其状态widget的state详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Flutter中如何加载并预览本地的html文件的方法

    Flutter中如何加载并预览本地的html文件的方法

    这篇文章主要介绍了Flutter中如何加载并预览本地的html文件的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11

最新评论