首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BottomNavigationView on fragment

BottomNavigationView on fragment
EN

Stack Overflow用户
提问于 2020-08-28 22:45:09
回答 1查看 52关注 0票数 1

我使用底部导航视图,在片段上它不能很好地工作,问题是没有过渡,它只显示第一个选定的

代码语言:javascript
复制
navigationView.setSelectedItemId(R.id.message_menu_Friend);

我的代码

公共类MessageFragment扩展了片段{

代码语言:javascript
复制
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView =inflater.inflate(R.layout.fragment_message, container, false);

    BottomNavigationView navigationView = rootView.findViewById(R.id.top_navigation_bar);
    navigationView.setOnNavigationItemSelectedListener(topNav);
    navigationView.setSelectedItemId(R.id.message_menu_Friend);
    getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M,new FriendFragment()).commit();
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_message, container, false);
}


private BottomNavigationView.OnNavigationItemSelectedListener topNav = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedItem = null;
        switch (item.getItemId()) {
            case R.id.message_menu_message:
                selectedItem = new MessagesFragment();
                break;
            case R.id.message_menu_Friend:
                selectedItem = new FriendFragment();
                break;

        }
        assert selectedItem != null;
        getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M,selectedItem).commit();
        return  true ;
    }
};}

Xml文件

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MessageFragment">



<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<FrameLayout android:layout_marginTop="112dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/hostFragment_M">

</FrameLayout>

<EditText
    android:id="@+id/editTextTextPersonSearch"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/White"
    android:backgroundTint="@color/White"
    android:drawableEnd="@drawable/ic_baseline_search_24"
    android:drawablePadding="8dp"
    android:ems="10"
    android:hint="@string/search"
    android:inputType="textPersonName"
    android:paddingStart="15dp"
    android:paddingEnd="15dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/top_navigation_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/White"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonSearch"
    app:menu="@menu/message_menu"
    />
  </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

如何使其在单击底部导航时显示片段

EN

回答 1

Stack Overflow用户

发布于 2020-08-28 23:22:51

删除行navigationView.setSelectedItemId(R.id.message_menu_Friend);,并将getChildFragmentManager()更改为getSupportFragmentManager()。此外,在onCreate方法中,只需返回rootView,而不是再次膨胀。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63636031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档