1. 程式人生 > >利用BottomNavigationBar實現不同的fragment之間的轉換

利用BottomNavigationBar實現不同的fragment之間的轉換

BE manage reat 隱藏 enc tom ica pre on()

我想要在三個頁面實現不同的東西,並且通過bottomNavigationBar進行切換

1、在build.gradle中引入bottom-navigation-bar

dependencies中加入
compile ‘com.ashokvarma.android:bottom-navigation-bar:1.3.1‘

2、創建三個fragmentActivity

其中之一

public class FragActivity_Me extends Fragment {

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
@Nullable Bundle savedInstanceState) { View view
= inflater.inflate(R.layout.fragment_3, container, false); return view; } }

3、在activity_maiin.xml中插入fragment

<LinearLayout
        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" android:orientation="vertical" tools:context=".TabBarActivity"> <FrameLayout android:id="@+id/fragment_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </
FrameLayout> <com.ashokvarma.bottomnavigation.BottomNavigationBar android:id="@+id/bottom_navigation_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> </LinearLayout>

4、修改其中一個FragmentActivity

public class FragActivity_Look extends Fragment {

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
return view;
}
}

5、修改MainActivity

首先public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener

申明變量

    private BottomNavigationBar bottomNavigationBar;
    private FrameLayout frameLayout;
    private FragmentTransaction transaction;
    private FragActivity_Look fragActivity_look;
    private FragActivity_Map fragActivity_map;
    private FragActivity_Me fragActivity_me;
    private Fragment mFragment;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout) findViewById(R.id.fragment_content);
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setActiveColor(R.color.colorAccent)//設置Item選中顏色方法
                .setInActiveColor(R.color.colorPrimary)//設置Item未選中顏色方法
                .setBarBackgroundColor("#FFFFFF");//背景顏色
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.bt_look, "Look").
                setActiveColorResource(R.color.teal)) .addItem(new BottomNavigationItem(R.mipmap.bt_map, "Map").
                setActiveColorResource(R.color.blue)) .addItem(new BottomNavigationItem(R.mipmap.bt_user, "Me").
                setActiveColorResource(R.color.grey)) .setFirstSelectedPosition(1) .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
        //initFragment();

        fragActivity_look=new FragActivity_Look();
        fragActivity_map = new FragActivity_Map();
        fragActivity_me = new FragActivity_Me();
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_content, fragActivity_map).commit();
        mFragment = fragActivity_map;
    }

切換fragment

private void switchFragment(Fragment fragment) {
        //判斷當前顯示的Fragment是不是切換的Fragment
        if(mFragment != fragment) {
            //判斷切換的Fragment是否已經添加過
            if (!fragment.isAdded()) {
                //如果沒有,則先把當前的Fragment隱藏,把切換的Fragment添加上
                getSupportFragmentManager().beginTransaction().hide(mFragment)
                        .add(R.id.fragment_content,fragment).commit();
            } else {
                //如果已經添加過,則先把當前的Fragment隱藏,把切換的Fragment顯示出來
                getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
            }
            mFragment = fragment;
        }
    }

實現OnTabSelectedListener接口所需要的方法

@Override
    public void onTabSelected(int position) {
        switch (position){
            case 0:
                switchFragment(fragActivity_look);
                break;
            case 1:
                switchFragment(fragActivity_map);
                break;
            case 2:
                switchFragment(fragActivity_me);
                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

還需要三個小圖片(24像素左右)及定義colors即可

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.fragment_content);
bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
bottomNavigationBar.setActiveColor(R.color.colorAccent)//設置Item選中顏色方法
.setInActiveColor(R.color.colorPrimary)//設置Item未選中顏色方法
.setBarBackgroundColor("#FFFFFF");//背景顏色
bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.bt_look, "Look").
setActiveColorResource(R.color.teal)) .addItem(new BottomNavigationItem(R.mipmap.bt_map, "Map").
setActiveColorResource(R.color.blue)) .addItem(new BottomNavigationItem(R.mipmap.bt_user, "Me").
setActiveColorResource(R.color.grey)) .setFirstSelectedPosition(1) .initialise();
bottomNavigationBar.setTabSelectedListener(this);
//initFragment();

fragActivity_look=new FragActivity_Look();
fragActivity_map = new FragActivity_Map();
fragActivity_me = new FragActivity_Me();
transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_content, fragActivity_map).commit();
mFragment = fragActivity_map;
}

利用BottomNavigationBar實現不同的fragment之間的轉換