BottomNavigationView的使用(结合Fragment)

实现的效果如下图,既可以切换下面的标签来切换fragment,也可以直接左右滑动切换fragment。

BottomNavigationView的使用(结合Fragment)_第1张图片BottomNavigationView的使用(结合Fragment)_第2张图片


1、首先当然是依赖导包

compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'

2、xml文件中布局

 
    


底部条的背景颜色    app :itemBackground= " @color/orange "

每个item的icon的颜色   app:itemIconTint="@color/white"

每个item的文字的颜色   app:itemTextColor="@color/white"

3、上面的控件还需要一个menu_bottom_nav.xml放置于res/menu中




    

    

    

    

4、初始化(包括Fragment、viewPager、 BottomNavigationView

    private void initView() {
        mBottomNav = (BottomNavigationView) findViewById(R.id.bottom_nav);
        mPager = (ViewPager) findViewById(R.id.viewpager);
        fragmentList = new ArrayList();

        mMovieFragment = MovieFragment.newInstance(mResources.getString(R.string.movie));
        mPhotoFragment = PhotoFragment.newInstance(mResources.getString(R.string.photo));
        mUiFragment = UiFragment.newInstance(mResources.getString(R.string.ui));
        mNetworkFragment = NetworkFragment.newInstance(mResources.getString(R.string.network));
        fragmentList.add(mMovieFragment);
        fragmentList.add(mPhotoFragment);
        fragmentList.add(mUiFragment);
        fragmentList.add(mNetworkFragment);
        //给ViewPager设置适配器
        MainFragmentpagerAdapter adapter = new MainFragmentpagerAdapter(getSupportFragmentManager(), fragmentList);
        mPager.setAdapter(adapter);
        mPager.setCurrentItem(0);//设置当前显示标签页为第一页
    }

5、 MainFragmentpagerAdapter
package com.example.doubanrxjava2demo.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by gd02881 on 2017/7/4.
 */

public class MainFragmentpagerAdapter extends FragmentPagerAdapter {

    private List fragList;

    public MainFragmentpagerAdapter(FragmentManager fm, List fragList) {
        super(fm);
        this.fragList = fragList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragList.get(position);
    }

    @Override
    public int getCount() {
        return fragList.size();
    }
}


6、事件监听

 private void setListener() {
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mBottomNav.setSelectedItemId(R.id.bottom_nav_movie);
                        break;
                    case 1:
                        mBottomNav.setSelectedItemId(R.id.bottom_nav_photo);
                        break;
                    case 2:
                        mBottomNav.setSelectedItemId(R.id.bottom_nav_ui);
                        break;
                    case 3:
                        mBottomNav.setSelectedItemId(R.id.bottom_nav_net);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.bottom_nav_movie:
                        mPager.setCurrentItem(0);
                        break;
                    case R.id.bottom_nav_photo:
                        mPager.setCurrentItem(1);
                        break;
                    case R.id.bottom_nav_ui:
                        mPager.setCurrentItem(2);
                        break;
                    case R.id.bottom_nav_net:
                        mPager.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });

    }


你可能感兴趣的:(android)