AndroidUtilCode的FragmentUtils.java

其示例代码的路径是https://github.com/Blankj/AndroidUtilCode/blob/master/app/src/main/java/com/blankj/androidutilcode/feature/core/fragment/FragmentActivity.java
实现方式有点复杂,因此我想写一个简单的DEMO, 场景是1个Activity+N个Fragment。

1 添加引用

 implementation 'com.blankj:utilcode:1.15.1'

在Application或者其他合适的地方初始化

Utils.init(getApplication());

2 FragmentAcitivity

布局文件




    

    


使用到menu文件navigation_fragment.xml



    
    
    

3 创建3个Fragment

fragment_root0.xml




    
    


fragment_root1.xml




    
    


fragment_root2.xml




    
    


4 代码部分

FragmentAcitivity.java

package com.prafly.rvdemo;

import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.Utils;

public class FragmentAcitivity extends AppCompatActivity {
    private BottomNavigationView navigation;
    private Fragment[] mFragments = new Fragment[3];
    private int curIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        Utils.init(getApplication());

        if (savedInstanceState != null) {
            curIndex = savedInstanceState.getInt("curIndex");
        }

        navigation = findViewById(R.id.navigation_fragment);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        mFragments[0] = Root0Fragment.newInstance();
        mFragments[1] = Root1Fragment.newInstance();
        mFragments[2] = Root2Fragment.newInstance();
        FragmentUtils.add(getSupportFragmentManager(), mFragments, R.id.fragment_container, curIndex);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_fragment_zero:
                    showCurrentFragment(0);
                    return true;
                case R.id.navigation_fragment_one:
                    showCurrentFragment(1);
                    return true;
                case R.id.navigation_fragment_two:
                    showCurrentFragment(2);
                    return true;
            }
            return false;
        }
    };

    private void showCurrentFragment(int index) {
        FragmentUtils.showHide(curIndex = index, mFragments);
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putInt("curIndex", curIndex);
    }
}

Root0Fragment.java

package com.prafly.rvdemo;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Root0Fragment extends Fragment {


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


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

    public static Root0Fragment newInstance() {
        Bundle args = new Bundle();
        Root0Fragment fragment = new Root0Fragment();
        fragment.setArguments(args);
        return fragment;
    }

}

Root1Fragment.java

package com.prafly.rvdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Root1Fragment extends Fragment {
    public Root1Fragment() {
        // Required empty public constructor
    }


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

    public static Root1Fragment newInstance() {
        Bundle args = new Bundle();
        Root1Fragment fragment = new Root1Fragment();
        fragment.setArguments(args);
        return fragment;
    }
}

Root2Fragment.java

package com.prafly.rvdemo;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Root2Fragment extends Fragment {


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


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

    public static Root2Fragment newInstance() {
        Bundle args = new Bundle();
        Root2Fragment fragment = new Root2Fragment();
        fragment.setArguments(args);
        return fragment;
    }

}

5 效果图

rvdemo.gif

你可能感兴趣的:(AndroidUtilCode的FragmentUtils.java)