SearchView默认情况下不填充整个Actionbar,也不响应单击

问题描述 投票:1回答:1

我环顾四周,无法找到我正在寻找的东西。一点帮助将不胜感激。我正在尝试实现一个SearchWidget,如here所示。然而,我得到了一个奇怪的设置。搜索图标甚至没有显示,最右边有三个垂直点作为工具栏的一部分,当我点击它们时会出现一个搜索框。但点击它不会通过setOnClickListenersetOnQueryTextFocusChangeListener注册任何内容。任何帮助将非常感激。像这样:

toolbar when opening the app

popup search menu - doesn't do anything when I click on it

这就是我所拥有的

我的SearchActivity:

class SearchCategoryActivity : MvvmActivity<SearchCategoryViewModel>() {

companion object {

    private const val CATEGORY = "category"

    fun newIntent(context: Context): Intent {
        return Intent(context, SearchCategoryActivity::class.java)
    }
}

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
val listofCategories : List<Category>? = null
private lateinit var adapter: CategoryGroupAdapter
var browsingData : List<Category>? = null

override fun onCreate(savedInstanceState: Bundle?) {
    AndroidInjection.inject(this)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_search_category)
    setSupportActionBar(toolbar)
    val actionBar = supportActionBar
    actionBar?.setDisplayHomeAsUpEnabled(true)

    adapter = CategoryGroupAdapter(this)
    adapter.setOnClickListener { category, _ -> onCategoryClick(category) }
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.itemAnimator = DefaultItemAnimator()
    recyclerView.adapter = adapter

    toolbar.setNavigationOnClickListener { finish() }
    //clearImageView.setOnClickListener { searchEdiText.text = null }
    addFab.setOnClickListener { onAddFabClick() }


    viewModel.loadCategories.subscribe(this, object : FlowableSubscriber<List<Category>> {
        override fun onNext(data: List<Category>) {
            browsingData = data
            onLoadCategories(data)
        }

        override fun onComplete() {
            Timber.error { "onComplete" }
        }

        override fun onError(error: Throwable) {
            onLoadCategoriesFailed(error)
        }
    })

    LceAnimator.showLoading(loading, content, error)
    viewModel.loadCategories()
    System.out.println("Here")


}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the options menu from XML
    val inflater = menuInflater
    inflater.inflate(R.menu.menu_search, menu)

    // Get the SearchView and set the searchable configuration
    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    val searchView = menu.findItem(R.id.action_search).actionView as SearchView?

    // Assumes current activity is the searchable activity
    searchView?.setSearchableInfo(searchManager.getSearchableInfo(componentName))
    searchView?.setIconifiedByDefault(false) // Do not iconify the widget; expand it by default

    searchView?.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            System.out.println("clicked")
        }

    })
    searchView?.setOnQueryTextFocusChangeListener(object : View.OnFocusChangeListener {
        override fun onFocusChange(v: View?, hasFocus: Boolean) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

        }

    })

我的SearchActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:background="@color/background">

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->


    <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
    to prevent the dummy from receiving focus again -->


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/layout_loading" />

        <include layout="@layout/layout_search" />

        <include layout="@layout/layout_error" />

    </FrameLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我的searchable.xml:

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint" />

我的search_menu.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:id="@+id/action_search"
        android:actionViewClass="android.widget.SearchView"
        android:layout_width="match_parent"
        android:icon="@android:drawable/ic_search_category_default"
        android:showAsAction="always"
        android:title="@string/search"
        app:queryBackground="@color/background"/>
</menu>
android xml kotlin android-toolbar searchview
1个回答
0
投票

尝试添加app而不是android并使用v7

app:actionViewClass="android.support.v7.widget.SearchView"

另外,要使其可折叠:

app:showAsAction="always|collapseActionView"

而且不需要android:layout_width="match_parent"

© www.soinside.com 2019 - 2024. All rights reserved.