Androidd打造万能的Fragment切换帮助类

Fragment我的理解他是一个碎片,Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期
现在基本上每个App上都会有用到Fragmnet这里我写了一个Fragment切换的帮助类,代码很简单
直接上代码,里面有注释
FragmentHelper

public class FragmentHelper {
    private FragmentManager mFragmentManager;
    private int mContainerViewID;

    /**
     *
     * @param fragmentManager  管理类
     * @param containerViewId   布局容器
     */
    public FragmentHelper(@Nullable FragmentManager fragmentManager,@IdRes int containerViewId){

        this.mContainerViewID =containerViewId;
        this.mFragmentManager =fragmentManager;
    }

    /**
     * 添加Fragment
     * @param fragment  需要添加的Fragment
     */
    public void addFragment(Fragment fragment){
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.add(mContainerViewID,fragment);
        fragmentTransaction.commit();
    }

    /**
     * 切换保存数据Fragment
     * @param fragment 进行切换的Fragment
     */
    public void switchFragment(Fragment fragment){
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        for (Fragment cFragment:fragments) {
            //将集合中所有的fragment遍历出来然后全部隐藏以便后面展示
            fragmentTransaction.hide(cFragment);
        }

        //进行判断framgnet是否在List里面存在  如果不存在就添加
        if( !fragments.contains(fragment)){
            fragmentTransaction.add(mContainerViewID,fragment);
        }else {
            //如果存在就展示出来
            fragmentTransaction.show(fragment);
        }

        fragmentTransaction.commit();
    }
    //进项Fragment切换但每次都要刷新数据
    public void switchFragment_RefreshData(Fragment framgent){
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.replace(mContainerViewID,framgent);
        fragmentTransaction.commit();
    }

}

上面的就是Fragment切换的帮助类,下面贴出他的基本使用方法

  • 首先我们会在加载Activity的时候设置一个默认的Fragment用到FragmentHelp中的 addFragment()方法
  • 其次就是Fragment的切换方法
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout frameLayout;
    private TextView homeButton;
    private TextView proButton;
    private TextView mineButton;
    private FragmentHelper fragmentHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        FragmentManager fragmentManager = getSupportFragmentManager();
        //R.id.frame_Layout该控件要是ViewGroup或者ViewGroup的子类
        fragmentHelper =new FragmentHelper(fragmentManager,R.id.frame_Layout);
         //设置默认加载的Fragment
        fragmentHelper.addFragment(new HomeFragment());
    }
    //设置事件监听
    private void setListener() {
        homeButton.setOnClickListener(this);
        proButton.setOnClickListener(this);
        mineButton.setOnClickListener(this);
    }
    //初始化控件
    private void initView() {
        frameLayout = (FrameLayout) findViewById(R.id.frame_Layout);
        homeButton = (TextView) findViewById(R.id.home_Button);
        proButton = (TextView) findViewById(R.id.pro_button);
        mineButton = (TextView) findViewById(R.id.mine_button);
    }

     //点击事件
    @Override
    public void onClick(View v) {

        //判断点击了那个按钮就切换到那个Fragment上去
        switch (v.getId()){
            case R.id.home_Button:
                fragmentHelper.switchFragment(new HomeFragment());
                break;
            case R.id.pro_button:
                fragmentHelper.switchFragment(new ProFrgment());
                break;
            case R.id.mine_button:
                fragmentHelper.switchFragment(new MineFragment());
                break;
        }
    }
}

布局代码

<?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="com.example.as.fragmentswitch.MainActivity">

    <FrameLayout
        android:id="@+id/frame_Layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.01dp"
        android:background="#999"
        />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            >
            <TextView
                android:id="@+id/home_Button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="首页"
                android:gravity="center"
                android:textSize="15sp"
                />
            <TextView
                android:id="@+id/pro_button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="15sp"
                android:text="项目"
                />
            <TextView
                android:id="@+id/mine_button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="15sp"
                android:text="我的"
                />

        </LinearLayout>
</LinearLayout>

几个Fragment我就不贴出来了非常简单
还有就是下面按钮的状态我也没有写出来也非常简单

猜你喜欢

转载自blog.csdn.net/liao5214/article/details/70546013
今日推荐