android java.util_AndroidUtilCode的FragmentUtils.java

1 添加引用

implementation 'com.blankj:utilcode:1.15.1'

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

Utils.init(getApplication());

2 FragmentAcitivity

布局文件

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">

android:id="@+id/fragment_container"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1" />

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"/>

使用到menu文件navigation_fragment.xml

android:id="@+id/navigation_fragment_zero"

android:icon="@drawable/ic_launcher_background"

android:title="0" />

android:id="@+id/navigation_fragment_one"

android:icon="@drawable/ic_launcher_background"

android:title="1" />

android:id="@+id/navigation_fragment_two"

android:icon="@drawable/ic_launcher_background"

android:title="2" />

3 创建3个Fragment

fragment_root0.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".Root0Fragment">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="fragment1" />

fragment_root1.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".Root1Fragment">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="fragment2" />

fragment_root2.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".Root2Fragment">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="fragment3" />

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 效果图

b59a3ee3805e

rvdemo.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值