android--导航(BottomNavigationBar)

    xiaoxiao2021-03-25  143

    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 文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/fragment_content"/> <com.ashokvarma.bottomnavigation.BottomNavigationBar android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/bottomNavigationBar"> </com.ashokvarma.bottomnavigation.BottomNavigationBar> </RelativeLayout>fragment_content.xml 文件<FrameLayout 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:id="@+id/frame_sub_content" android:background="@color/colorYello"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/tv_fragment_conten" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> 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' }

    转载请注明原文地址: https://ju.6miu.com/read-13102.html

    最新回复(0)