栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在BottomNavigationView中开始片段

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在BottomNavigationView中开始片段

首先,在您的activity_main.xml中,我们不需要3个不同的片段,因为我们只能在1个frameLayout中替换或添加任何选定的片段。其次,只要用户从Bottom
NavigationView中选择任何一个,就只需获取相关Fragment类的实例,并将其替换为activity_main的frameLayout即可。

Fragment selectedFragment = null;         switch (item.getItemId()) {    case R.id.navigation_home:        selectedFragment = FunFragment.newInstance();        break;

在获得选定片段的实例后,将其替换为activity_main的frameLayout,以显示在屏幕上。

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.content, selectedFragment); transaction.commit();

编辑标记 步骤1. 您的activity_main.xml应该如下所示

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.exampl.MainActivity">    <frameLayout        android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"/>    <android.support.design.widget.BottomNavigationView        android:id="@+id/navigation"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="bottom"        android:background="?android:attr/windowBackground"        app:menu="@menu/navigation" /></LinearLayout>

步骤2. 您的fragment_home.xml布局应如下所示

 <?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical" android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="android"/>    </LinearLayout>

为您的三个不同选项制作3种不同的片段布局

步骤3. 您的MainActivity.java类将如下所示

private BottomNavigationView.onNavigationItemSelectedListener monNavigationItemSelectedListener     = new BottomNavigationView.onNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) {     Fragment selectedFragment = null;     switch (item.getItemId()) {         case R.id.navigation_home:  selectedFragment = FunFragment.newInstance(); break;         case R.id.navigation_dashboard:  selectedFragment = TheoryFragment.newInstance(); break;         case R.id.navigation_notifications:  selectedFragment = AndroidFragment.newInstance();  break;     }     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();     transaction.replace(R.id.content, selectedFragment);     transaction.commit();     return true; }        };@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        navigation.setonNavigationItemSelectedListener(mOnNavigationItemSelectedListener);        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();        transaction.replace(R.id.content, FunFragment.newInstance());        transaction.commit();    }

在on create上替换为要在启动应用程序后显示的片段,导航监听器将照顾您将选择的任何选项

步骤4. 您将拥有3个不同的Fragment类,如下所示

public class TheoryFragment extends Fragment {     public static TheoryFragment newInstance() { TheoryFragment fragment = new TheoryFragment(); return fragment;        }     @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_theory, container, false); unbinder = ButterKnife.bind(this, rootView); return rootView;        }    }

希望它能对您有所帮助,如有任何问题,请告诉我。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号