Android BottomNavigationBar 底部导航栏

ViewPager实现   

 

 

compile 'com.ashokvarma.android:bottom-navigation-bar:2.0.4'//底部导航栏

 




    

    
    
    


package jiyun_zhoukao_demo1.com.myapplication.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jiyun_zhoukao_demo1.com.myapplication.R;
import jiyun_zhoukao_demo1.com.myapplication.adapter.SectionsPagerAdapter;
import jiyun_zhoukao_demo1.com.myapplication.fragment.BlankFragment;
import jiyun_zhoukao_demo1.com.myapplication.fragment.BlankFragment2;
import jiyun_zhoukao_demo1.com.myapplication.fragment.BlankFragment3;
import jiyun_zhoukao_demo1.com.myapplication.fragment.BlankFragment4;
import jiyun_zhoukao_demo1.com.myapplication.fragment.BlankFragment5;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener, ViewPager.OnPageChangeListener {


    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    private List fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initView() {
        initViewPager();
    }

    private void initViewPager() {
        fragments = new ArrayList();
        fragments.add(new BlankFragment());
        fragments.add(new BlankFragment2());
        fragments.add(new BlankFragment3());
        fragments.add(new BlankFragment4());
        fragments.add(new BlankFragment5());

        viewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);
    }

    private void initData() {
        bottomNavigationBar.setTabSelectedListener(this);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.shouye1, null).setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.shouye2)))//非选中的图片)
                .addItem(new BottomNavigationItem(R.drawable.fenglei1, null).setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.fenglei2)))
                .addItem(new BottomNavigationItem(R.drawable.vip_sy, null).setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.vip_sy)))
                .addItem(new BottomNavigationItem(R.drawable.kebiao1, null).setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.kebiao2)))
                .addItem(new BottomNavigationItem(R.drawable.mine_ch, null).setInactiveIcon(ContextCompat.getDrawable(this, R.drawable.mine_no)))
                .setFirstSelectedPosition(0)

           /*     .setActiveColor("#EA5FFE")//#F6F0F0白色
                .setInActiveColor("#F6F0F0")//则色*/
                .setBarBackgroundColor("#F6F0F0")//#FD8822//黄色
                .initialise();
    }

    @Override
    public void onTabSelected(int position) {
        //  点击Item时调用此方法
        viewPager.setCurrentItem(position);

    }

    @Override
    public void onTabUnselected(int position) {
        //  对没有选中的Item进行处理的方法,
    }

    @Override
    public void onTabReselected(int position) {
        // 当被选中的Item 再一次被点击时调用此方法
    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bottomNavigationBar.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
package jiyun_zhoukao_demo1.com.myapplication.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 Lenovo on 2018/7/10.
 *
 * @ClassName:SectionsPagerAdapter
 */

public class SectionsPagerAdapter extends FragmentPagerAdapter{
    List fragments;

    public SectionsPagerAdapter(FragmentManager fm, List fragments) {
        super(fm);
        this.fragments = fragments;
    }

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

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

 

你可能感兴趣的:(Android BottomNavigationBar 底部导航栏)