AndroidUtilCode工具类库的部分使用

首先添加如下依赖:

compile 'com.blankj:utilcode:1.11.1'

其中FragmentUtils工具类的使用,还有很多工具类
mainactivity.java文件中

public class WaiXingAcitivity extends AppCompatActivity {
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;
    private FirstFragment firstFragment;
    private SecondFragment secondFragment;
    private ThridFragment thridFragment;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waixing);
        navigationView = (NavigationView) findViewById(R.id.navi);
        drawerLayout = (DrawerLayout) findViewById(R.id.draw);
        firstFragment = FirstFragment.getInstance();
        secondFragment = SecondFragment.getInstance();
        thridFragment = ThridFragment.getInstance();
        fragmentManager = getSupportFragmentManager();
        FragmentUtils.add(fragmentManager, firstFragment, R.id.zaiti);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.f:
                        Log.d("WaiXingAcitivity", "点击了");
                        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                            drawerLayout.closeDrawer(GravityCompat.START);
                        }
                        List list = new ArrayList();
                        list.add(secondFragment);
                        list.add(thridFragment);
                        FragmentUtils.showHide(firstFragment, list);
                        break;
                    case R.id.f1:
                        Log.d("WaiXingAcitivity", "点击了1");
                        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                            drawerLayout.closeDrawer(GravityCompat.START);
                        }
                        if (FragmentUtils.findFragment(fragmentManager, SecondFragment.class) == null) {
                            if (FragmentUtils.findFragment(fragmentManager, ThridFragment.class) != null)
                                FragmentUtils.hide(thridFragment);
                            FragmentUtils.hide(firstFragment);
                            FragmentUtils.add(fragmentManager, secondFragment, R.id.zaiti);
                        } else {
                            List list1 = new ArrayList();
                            list1.add(firstFragment);
                            list1.add(thridFragment);
                            FragmentUtils.showHide(secondFragment, list1);
                        }
                        break;
                    case R.id.f2:
                        Log.d("WaiXingAcitivity", "点击了2");
                        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                            drawerLayout.closeDrawer(GravityCompat.START);
                        }
                        if (FragmentUtils.findFragment(fragmentManager, ThridFragment.class) == null) {
                            if (FragmentUtils.findFragment(fragmentManager, SecondFragment.class) != null)
                                FragmentUtils.hide(secondFragment);
                            FragmentUtils.hide(firstFragment);
                            FragmentUtils.add(fragmentManager, thridFragment, R.id.zaiti);
                        } else {
                            List list2 = new ArrayList();
                            list2.add(firstFragment);
                            list2.add(secondFragment);
                            FragmentUtils.showHide(thridFragment, list2);
                        }
                        break;
                }
                return true;
            }
        });
    }
}

在waixing.xml文件中




    

    

在FirstFragment.java文件中

public class FirstFragment extends Fragment {
    private static FirstFragment firstFragment;
    public static FirstFragment getInstance(){
        if(firstFragment==null){
            firstFragment=new FirstFragment();
        }
        return firstFragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.first,container,false);
        return view;
    }
}

相关文章:
https://github.com/Blankj/AndroidUtilCode
https://blankj.com/2016/07/31/android-utils-code/

你可能感兴趣的:(AndroidUtilCode工具类库的部分使用)