【问题标题】:How to open search interface over BottomNavigationView's fragment?如何在 BottomNavigationView 的片段上打开搜索界面?
【发布时间】:2022-01-15 02:15:16
【问题描述】:

我想实现一个具有 BottomNavigationView 的界面,单击顶部搜索栏会打开下一个界面,如图 2 所示。我已经实现了 BottomNavigationView。但是上面说的做不到。

代码如下:

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        BottomNavigationView bottomNavigationView = findViewById(R.id.home_bottom_nav_view);
        bottomNavigationView.setSelectedItemId(R.id.home_recent_menu_id);
        

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                Fragment selectedFragment = new RecentFragment();

                switch (item.getItemId()) {

                    case R.id.home_contact_menu_id:
                        selectedFragment = new ContactFragment();
                        break;

                    case R.id.home_recent_menu_id:
                        selectedFragment = new RecentFragment();
                        break;

                    case R.id.home_status_menu_id:
                        selectedFragment = new StatusFragment();
                        break;

                }

                getSupportFragmentManager().beginTransaction().replace(R.id.home_fragment_container_lyt_id, selectedFragment).commit();
                return true;

            }
        });


    }


}

主页活动 XML 代码

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/home_search_view_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <FrameLayout
        android:id="@+id/home_fragment_container_lyt_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/home_search_view_id"
        android:layout_above="@+id/home_bottom_nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/home_bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu"
        android:layout_alignParentBottom="true"
        />


</RelativeLayout>

【问题讨论】:

    标签: android android-viewpager bottomnavigationview android-search android-bottomnav


    【解决方案1】:
    1. 创建一个BottomNavigationFragment,它将包含ViewPager2BottomNavigationView,并且该视图寻呼机将包含[favourite, recent and contacts ] 片段

    2. 创建一个SearchFragment,用于搜索目的

    3. 创建一个导航图,当用户点击搜索视图时将包含[BottomNavigationFragment and SearchFragment] 导航到SearchFragment

    您还可以使用导航组件库设置动画

    //ViewPager2 vp2 in BottomNavigationFragment 的适配器

    假设您的 SearchViewfavourite 片段 onClick 或 onFocus 中

    findNavController().navigate(R.id.toSearchFragment)
    

    您将进入搜索片段

    【讨论】:

    • 你能告诉我任何示例链接吗?
    • 我已经更新了答案,让我知道你需要链接了解更多的部分
    • 我已经更新了我的问题。请告诉我该怎么做。
    • 1.而不是 Activity 使用 Fragment 并将 xml 放在该片段“BottomNavigationFragment”中 2. 使用 ViewPager2 和 FragmentStatePageAdapter 更改框架布局 3. 创建另一个名为 search fragment 的片段 4. 现在在活动中使用来自 google 的 NavigationComponentLib
    • youtube.com/…看这个播放列表你会更好的理解
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2022-08-18
    相关资源
    最近更新 更多