简体   繁体   English

在 Kotlin 中自动展开搜索查看

[英]Expanding searchView Automatically in Kotlin

I have a searchView in my IndexActivity .我的IndexActivity中有一个 searchView 。 On clicking the searchView a new activity is started called SearchActivity .单击 searchView 时,将启动一个名为SearchActivity的新活动。 Inside the search activity again, I have the searchView which expands on clicking.再次在搜索活动中,我有在单击时展开的 searchView。 It has a query listener which does an external API call.它有一个查询侦听器,它执行外部 API 调用。

However, after I open the SearchActivity by clicking on searchView icon, I have to again click on searchView to type in it.但是,通过单击 searchView 图标打开 SearchActivity 后,我必须再次单击 searchView 以输入它。 What I want is searchView should be focussed as soon as the activity starts.我想要的是 searchView 应该在活动开始后立即关注。 This is my code of searchactivity:这是我的搜索活动代码:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar_search, menu)
        val searchView: SearchView =
            menu?.findItem(R.id.menu_search)?.actionView as SearchView
        // searchView.setIconifiedByDefault(false)
        // searchView.focusable = View.FOCUSABLE
        // searchView.isIconified = false
        // searchView.requestFocusFromTouch()
        // searchView.onActionViewExpanded()

The commented lines are the ones I tried by searching on the web but it doesn't seem to work.注释行是我通过搜索 web 尝试的行,但它似乎不起作用。

This is my code in toolbar_searchbar.xml which is included in activity_search set by SearchActivity:这是我在toolbar_searchbar.xml中的代码,它包含在SearchActivity设置的activity_search中:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextColor="@color/cardview_light_background"
            app:subtitleTextAppearance="@font/alegreya_sans_medium"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:background="@android:color/transparent"
            android:id="@+id/toolbar_searchbar"
            app:popupTheme="@style/AppTheme.PopupOverlay">
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

This is the code of toolbar_search.xml which is inflated as seen in the code provided above:这是toolbar_search.xml的代码,如上面提供的代码所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:title="Search"
        android:id="@+id/menu_search"
        android:icon="@drawable/quantum_ic_search_grey600_24"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        />

</menu>

This is what I get at the start.这是我一开始得到的。

这是我在活动开始时得到的

This is what I want at the start.这就是我一开始想要的。

这就是我要的

Make sure to import: androidx.appcompat.widget.SearchView确保导入: androidx.appcompat.widget.SearchView

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search_contacts"
        android:title="search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="androidx.appcompat.widget.SearchView" >
    </item>
</menu>
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_view, menu)
    val searchView: SearchView =
        menu?.findItem(R.id.search_contacts)?.actionView as SearchView

    searchView.isIconified = false
    return true
}

It would be much easier for you, to have everything on one activity and have only one toolbar.对您来说,将所有内容都放在一个活动上并且只有一个工具栏会容易得多。 You don't need to create another activity just to start another logic, it shouldn't be bound with the UI.您不需要创建另一个活动来启动另一个逻辑,它不应该与 UI 绑定。

So your solution is to create a container fragment (if you need a few screens), add a toolbar on top of it and that's all.因此,您的解决方案是创建一个容器片段(如果您需要几个屏幕),在其顶部添加一个工具栏,仅此而已。 Move logic to a ViewModel and do all logic there.将逻辑移动到 ViewModel 并在那里执行所有逻辑。

By this approach you will solve a few problems:通过这种方法,您将解决一些问题:

  • You won't have blinks while screen change换屏时不会眨眼
  • Keyboard handling (open/closed)键盘处理(打开/关闭)
  • You can easier and more correctly control toolbar and focus for SearchView您可以更轻松、更正确地控制 SearchView 的工具栏和焦点
  • You don't solve a problem of an incorrect approach but implements everything in better way您不会解决方法不正确的问题,而是以更好的方式实现一切

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM