android--导航(BottomNavigationBar)

MainActivty->(Fragment)->
NavigationFragment->BottomNavigtionBar实现底部导航->(Fragment)
->Fragment1 (具体的页)
->Fragment2(具体的页)
->Fragment3(具体的页)
->Fragment4 (具体的页)



第一步:创建 NavigationFragment

 
public class NavigationFragment extends Fragment implements BottomNavigationBar.OnTabSelectedListener {

    HomeFragment homeFragment;
    LikeFragment likeFragment;
    LocationFragment locationFragment;
    PersonFragment personFragment;
    public static NavigationFragment newInstance(String str){
        NavigationFragment navigationFragment=new NavigationFragment();
        Bundle bundle=new Bundle();
        bundle.putString(Constants.ARGS,str);
        navigationFragment.setArguments(bundle);
        return  navigationFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_bottom_navigation_bar,container,false);
        TextView  mTextView = (TextView) view.findViewById(R.id.tv_fragment_conten);
        Bundle bundle= getArguments();
        if(bundle!=null){
            String str=bundle.getString(Constants.ARGS);
            if(!TextUtils.isEmpty(str)){
                mTextView.setText(str);
            }
        }
        //--------------------BottomNavigationBar-------------------------------------------------
        BottomNavigationBar bottomNavigationBar=(BottomNavigationBar)view.findViewById(R.id.bottomNavigationBar);
        bottomNavigationBar.setTabSelectedListener(this);
        bottomNavigationBar.setBackgroundStyle(bottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.home_fill,"home")//选中的状态的图片
                 .setInactiveIconResource(R.drawable.home)//没有选中的状态的图标
                 .setActiveColorResource(R.color.colorPrimary)//选中的字体颜色
                 .setInActiveColorResource(R.color.black_1));//没有选中的字体颜色

        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.location_fill,"location")
                .setInactiveIconResource(R.drawable.location)
                .setActiveColorResource(R.color.colorPrimary)
                .setInActiveColorResource(R.color.black_1));

        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.like_fill,"like")
                .setInactiveIconResource(R.drawable.like)
                .setActiveColorResource(R.color.colorPrimary)
                .setInActiveColorResource(R.color.black_1));

        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.person_fill,"person")
                .setInactiveIconResource(R.drawable.person)
                .setActiveColorResource(R.color.colorPrimary)
                .setInActiveColorResource(R.color.black_1));

        bottomNavigationBar.setFirstSelectedPosition(0);
        bottomNavigationBar.initialise();
        bottomNavigationBar.setTabSelectedListener(this);
        //----------------------BottomNavigationBar-----------------------------------------------
        swithPage(0);//默认显示home
        return  view;  // return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onTabSelected(int position) {
        swithPage(position);
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    private  void  swithPage(int position){
        FragmentTransaction transaction=getFragmentManager().beginTransaction();
        switch (position){
            case  0:
                if(homeFragment==null)
                    homeFragment=homeFragment.newInstance("Home","");
                transaction.replace(R.id.frame_sub_content,homeFragment).commit();
                break;
            case 1:
                if(locationFragment==null)
                    locationFragment=locationFragment.newInstance("location","");
                transaction.replace(R.id.frame_sub_content,locationFragment).commit();
                break;
            case 2:
                if(likeFragment==null)
                    likeFragment=likeFragment.newInstance("location","");
                transaction.replace(R.id.frame_sub_content,likeFragment).commit();
                break;
            case 3:
                if(personFragment==null)
                    personFragment=personFragment.newInstance("location","");
                transaction.replace(R.id.frame_sub_content,personFragment).commit();
                break;
        }
    }
}
fragment_bottom_navigation_bar.xml 文件
 
  
 
  



     

     
     

fragment_content.xml 文件


    
    



MainActivty
 
  
 
  
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setCurrentFragment();
    }

  private void setCurrentFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        mNavigationFragment = NavigationFragment.newInstance("one");
        transaction.replace(R.id.frame_content, mNavigationFragment).commit();
    }


 
  
 
  
 
  
 
  

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    // compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.1'
    compile 'com.android.support:support-v4:25.2.0'
    testCompile 'junit:junit:4.12'
}

你可能感兴趣的:(android--导航(BottomNavigationBar))