美文网首页
AndroidUtilCode的FragmentUtils.ja

AndroidUtilCode的FragmentUtils.ja

作者: 941疯子 | 来源:发表于2018-05-17 11:39 被阅读0次

其示例代码的路径是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

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation_fragment"/>

</LinearLayout>

使用到menu文件navigation_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_fragment_zero"
        android:icon="@drawable/ic_launcher_background"
        android:title="0" />
    <item
        android:id="@+id/navigation_fragment_one"
        android:icon="@drawable/ic_launcher_background"
        android:title="1" />
    <item
        android:id="@+id/navigation_fragment_two"
        android:icon="@drawable/ic_launcher_background"
        android:title="2" />
</menu>

3 创建3个Fragment

fragment_root0.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Root0Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment1" />

</FrameLayout>

fragment_root1.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Root1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment2" />

</FrameLayout>

fragment_root2.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Root2Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment3" />

</FrameLayout>

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.ja

      本文链接:https://www.haomeiwen.com/subject/xrqbdftx.html