【问题标题】:Pass SearchView Query From Starting Fragment to Another Fragment将 SearchView 查询从起始片段传递到另一个片段
【发布时间】:2019-12-19 20:46:19
【问题描述】:

在我的 Fragment 类中,我有这个:

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.my_menu, menu)
        val searchItem = menu.findItem(R.id.action_search)
        searchView = searchItem.actionView as SearchView
        searchView.apply{
            queryHint = "Search"
            isIconified = false
            setOnQueryTextListener(object : SearchView.OnQueryTextListener{
                override fun onQueryTextSubmit(query: String): Boolean {
                    // pass the query to the other fragment and navigate to it
                    findNavController().navigate(GroupListFragmentDirections.actionGroupListFragmentToSearchedGroupListFragment(query))
                    return false
                }

                override fun onQueryTextChange(newText: String): Boolean {
                    // start the search
                    //groupListViewModel.searchGroups(newText)
                    return false
                }
            })
        }
    }

现在,我想将查询传递给另一个片段并导航到它。然后另一个片段以某种方式处理查询。 但 Android Studio 告诉我 SearchView 没有设置 NavController。 这是我在 LogCat 中找到的错误消息:

java.lang.IllegalStateException: View androidx.appcompat.widget.SearchView{c83b4ef VFE...... ........ 147,10-987,136 #7f08003d app:id/action_search} does not have a NavController set
        at androidx.navigation.Navigation.findNavController(Navigation.java:84)
        at androidx.navigation.ViewKt.findNavController(View.kt:28)
        at com.celik.abdullah.mylim4.ui.GroupListFragment$onCreateOptionsMenu$1$1.onQueryTextSubmit(GroupListFragment.kt:93)
        at androidx.appcompat.widget.SearchView.onSubmitQuery(SearchView.java:1191)
        at androidx.appcompat.widget.SearchView$7.onEditorAction(SearchView.java:1168)
        at android.widget.TextView.onEditorAction(TextView.java:5911)
        at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
...

我使用 Android 架构组件中的 NavigationUI 库。 MainActivity 的布局包含 NavHostFragment 布局小部件。您在上面看到的片段代码是起始片段(用户在应用打开时看到的片段)。

我假设不允许在搜索视图的侦听器中使用 findNavController()。 不过,是否有一种方法可以将查询从搜索视图传递到下一个片段。

这是我的活动布局:

// activity_main.xml
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <fragment
            android:id="@+id/myNavHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation" />

</LinearLayout>

这里是我的 navigation.xml:

<navigation 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/navigation"
    app:startDestination="@id/groupListFragment">

    <fragment
        android:id="@+id/groupListFragment"
        android:name="com.celik.abdullah.mylim4.ui.GroupListFragment"
        android:label="GroupListFragment"
        tools:layout="@layout/fragment_group_list">
        <action
            android:id="@+id/action_groupListFragment_to_searchedGroupListFragment"
            app:destination="@id/searchedGroupListFragment" />
    </fragment>
    <fragment
        android:id="@+id/searchedGroupListFragment"
        android:name="com.celik.abdullah.mylim4.ui.SearchedGroupListFragment"
        android:label="SearchedGroupListFragment"
        tools:layout="@layout/fragment_searched_group_list">
        <argument
            android:name="query"
            app:argType="string" />
    </fragment>
</navigation>

【问题讨论】:

  • 您可以发布您的导航 xml 和包含导航的活动 xml 吗?
  • @Christilyn:我已经添加了它们。

标签: android android-fragments navigation searchview


【解决方案1】:

如果您在Toolbar 中使用SearchView,则应使用导航设置Toolbar

override fun onCreate(savedInstanceState: Bundle?) {
    setContentView(R.layout.activity_main)

    ...

    val navController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = AppBarConfiguration(navController.graph)
    findViewById<Toolbar>(R.id.toolbar)
        .setupWithNavController(navController, appBarConfiguration)
}

同样适用于ActionBar

setupActionBarWithNavController(navController, appBarConfiguration)

查看此链接了解更多详情:https://developer.android.com/guide/navigation/navigation-ui#appbarconfiguration

【讨论】:

    猜你喜欢
    • 2020-05-31
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多