关于Fragment的学习与使用

一、什么是Fragment

1.基于Android系统的设备越来越多,分辨率种类也越来越多,Google提出Fragment的概念也是希望通过Fragment解决局部碎片化问题;

2.Fragment翻译为碎片,自Android开始引入Fragment的概念,Fragment最初由Google打包到v4的支持包中,安卓4.0之后纳入到Android SDK中;

3.用Fragment替换是TabHost是Google推荐的方案。

二、用Fragment能解决什么问题

1.可以在一个Activity中实现不同界面的灵活切换

2.解决Activity之间切换不流畅

3.可封装成不同的重要组件,并可单独管理其生命周期和UI布局

4.无需在AndroidManifest中注册,可在布局文件中直接引用

三、Fragment静态加载方法(附代码)

1.首先新建一个类继承Fragment

public class FragmentOne extends Fragment {


    public FragmentOne() {
        // Required empty public constructor
    }

2.重写onCreateView方法

3.使用Layoutlnflater对象中的inflate方法绑定布局和控件

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_one, container, false);
    return view;
}

4.在Activity对应的布局文件中通过<fragment>标签引用

<fragment
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/f_one"
    android:name="com.example.myapplication.fragment.FragmentOne" />

四、Fragment动态加载方法(附代码)

1.首先新建一个类继承Fragment

public class NvFragment extends Fragment {


    public NvFragment() {
        // Required empty public constructor
    }

2.重写onCreateView方法

3.使用Layoutlnflater对象中的inflate方法绑定布局和控件

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_nv, container, false);
    return view;
}

4.使用FragmentManager和FragmentTransaction对象进行动态加载

public void onClick(View view) {
    FragmentManager manager=getFragmentManager();//创建FragmentManger对象
    FragmentTransaction transaction=manager.beginTransaction();//创建FragmentTransaction对象
  switch (view.getId()){
      case R.id.nvzhuang_btn:
          if (nvFragment==null){
              nvFragment=new NvFragment();
          }
          transaction.replace(R.id.shop_content,nvFragment);
          break;
      case R.id.nanzhuang_btn:
          if (nanFragment==null){
              nanFragment=new NanFragment();
          }
          transaction.replace(R.id.shop_content,nanFragment);
          break;
          default:
              break;
  }
  transaction.commit();//最后一步要提交
}

补充:静态加载的优点:首先把Activity当做控件使用,使用简单;第二个则是可以在Activity中直接变更Fragment布局中的控件。     缺点:不能根据动态逻辑动态加载Fragment。

动态加载的优点:可以根据业务逻辑动态的加载Fragment;   缺点:在代码的实现上,相比静态加载,难度大一些。

五、ViewPager+Fragment实现页卡滑动切换(附代码)

1.首先在总行布局里定义一个ViewPager(v4包),在里面定义相关属性;

<android.support.v4.view.ViewPager
    android:id="@+id/wx_vp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/wx_bottom">
</android.support.v4.view.ViewPager>
2.定义一个adapter包,在里面创建一个MyPagerAdapter适配器来继承FragmentPagerAdapter,然后alt加回车,会跳出方法。在这边需要注意的是要在MyPagerAdapter方法里加一句List<Fragment> fragmentList。同时要在外面定义一句局部变量 private List<Fragment> mFragmentList,这是很重要的。

public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> mFragmentList;

    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.mFragmentList=fragmentList;
    }


    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}

3.最后回到主页面中来定义ViewPager和List<Fragment>,同时定义出三个子页面来配合主页面来进行滑动。然后给他们分别绑定ID和设置监听,最重要的是要将三个子页面填充进fragmentList里。最后一步就是创建适配器对象和绑定适配器。

 private ViewPager viewPager;

 private ContactFragment contactFragment;
 private FriendFragment friendFragment;
 private NewsFragment newsFragment;

 private List<Fragment> fragmentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wx);

    bindId();

    contactFragment=new ContactFragment();
    friendFragment=new FriendFragment();
    newsFragment=new NewsFragment();

    fragmentList.add(contactFragment);
    fragmentList.add(friendFragment);
    fragmentList.add(newsFragment);

    //创建适配器对象
    MyPagerAdapter adapter=new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
    //绑定适配器
    viewPager.setAdapter(adapter);
}

private void bindId() {
    contactBtn=findViewById(R.id.contact_btn);
    friendBtn=findViewById(R.id.friend_btn);
    newsBtn=findViewById(R.id.news_btn);
    viewPager=findViewById(R.id.wx_vp);

    contactBtn.setOnClickListener(this);
    friendBtn.setOnClickListener(this);
    newsBtn.setOnClickListener(this);
}
知识点:1.在Fragment导包时要注意v4包和app包的匹配

2.commit方法:在动态加载中,当所有的准备工作完成后,必须调用commit方法才能执行和生效;相当于Toast中的show方法,不调用此方法,不会执行。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值