首页 > 学院 > 开发设计 > 正文

Fragment的隐藏与显示

2019-11-09 18:09:35
字体:
来源:转载
供稿:网友

一般fragment和activity一起用的时候有两种方法,一个是用fragment替换到activity里事先放置的站位布局

FragmentTransaction fragmentTransaction = getFragmentTransaction(); fragmentTransaction.replace(res, fragment); fragmentTransaction.commit();

但是这种方法就是每次都要创建一个fragment去替换到布局里面去,每次都要把fragment里面的代码都走一遍,如果是有很多数据的网络请求,每次都要去请求一遍,是不是很耗时间和流量,解决的办法就是用fragment的显示与隐藏 看看效果这里写图片描述

下面是实现的代码

package com.duanlian.fragmenthideandshowdemo;import android.os.Bundle;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.widget.RadioGroup;import com.duanlian.fragmenthideandshowdemo.fragment.MaFragment;import com.duanlian.fragmenthideandshowdemo.fragment.WangFragment;import com.duanlian.fragmenthideandshowdemo.fragment.ZhangFragment;import com.duanlian.fragmenthideandshowdemo.fragment.ZhaoFragment;public class MainActivity extends AppCompatActivity { PRivate RadioGroup mRadioGroup;//单选组 private FragmentTransaction mFragmentTransaction;//fragment事务 private FragmentManager mFragmentManager;//fragment管理者 private WangFragment mWangFragment; private MaFragment mMaFragment; private ZhangFragment mZhangFragment; private ZhaoFragment mZhaoFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setClick(0);//默认进去显示王超fragment } private void initView() { mRadioGroup = (RadioGroup) findViewById(R.id.rg); mFragmentManager = getSupportFragmentManager();//获取到fragment的管理对象 //RadioGroup的选中监听 mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_zero://王超 setClick(0); break; case R.id.rb_one://马汉 setClick(1); break; case R.id.rb_two://张龙 setClick(2); break; case R.id.rb_three://赵虎 setClick(3); break; } } }); } private void setClick(int type) { //开启事务 mFragmentTransaction = mFragmentManager.beginTransaction(); //显示之前将所有的fragment都隐藏起来,在去显示我们想要显示的fragment hideFragment(mFragmentTransaction); switch (type) { case 0://王超 //如果王超的fragment是null的话,就创建一个 if (mWangFragment == null) { mWangFragment = new WangFragment(); //加入事务 mFragmentTransaction.add(R.id.fragment, mWangFragment); } else { //如果王超fragment不为空就显示出来 mFragmentTransaction.show(mWangFragment); } break; case 1: if (mMaFragment == null) { mMaFragment = new MaFragment(); //加入事务 mFragmentTransaction.add(R.id.fragment, mMaFragment); } else { //如果王超fragment不为空就显示出来 mFragmentTransaction.show(mMaFragment); } break; case 2: if (mZhangFragment == null) { mZhangFragment = new ZhangFragment(); //加入事务 mFragmentTransaction.add(R.id.fragment, mZhangFragment); } else { //如果王超fragment不为空就显示出来 mFragmentTransaction.show(mZhangFragment); } break; case 3: if (mZhaoFragment == null) { mZhaoFragment = new ZhaoFragment(); //加入事务 mFragmentTransaction.add(R.id.fragment, mZhaoFragment); } else { //如果王超fragment不为空就显示出来 mFragmentTransaction.show(mZhaoFragment); } break; } //提交事务 mFragmentTransaction.commit(); } /** * 用来隐藏fragment的方法 * * @param fragmentTransaction */ private void hideFragment(FragmentTransaction fragmentTransaction) { //如果此fragment不为空的话就隐藏起来 if (mWangFragment != null) { fragmentTransaction.hide(mWangFragment); } if (mMaFragment != null) { fragmentTransaction.hide(mMaFragment); } if (mZhangFragment != null) { fragmentTransaction.hide(mZhangFragment); } if (mZhaoFragment != null) { fragmentTransaction.hide(mZhaoFragment); } }}

细节请看demo 点击下载


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表