简体   繁体   English

菜单不会显示在“活动”中

[英]Menu won't show in Activity

I have a menu that won't show up in my activity. 我的菜单不会显示在我的活动中。 I have a main activity and a separate activity that contains a list of items I want the user to be able to search through. 我有一个主要活动和一个单独的活动,其中包含一个我希望用户能够搜索的项目列表。 However, the menu isn't showing up in the activity for searching, so nothing happens when entering in a string for the search. 但是,该菜单未显示在搜索活动中,因此在输入搜索字符串时不会发生任何事情。

The goal was the get the menu in the search activity in order to allow the user to search through items using a SearchView and RecyclerView 目的是在搜索活动中获取菜单,以便允许用户使用SearchView和RecyclerView搜索项目

I'm very new to Android Studio, so any help would be greatly appreciated. 我是Android Studio的新手,因此不胜感激。

Here is the menu for the SearchView 这是SearchView的菜单

<?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:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        />

    <item android:id="@+id/action_search2"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        />


</menu>

Here is the layout xml 这是布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


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



    <android.support.v7.widget.SearchView
        android:id="@+id/action_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:menu="@menu/search_menu">


    </android.support.v7.widget.SearchView>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme">

        <Button
            android:id="@+id/GoBackbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GO BACK" />

        <!--<android.support.v7.widget.SearchView-->
            <!--android:id="@+id/searchBar"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content">-->
            <!--&lt;!&ndash;app:menu="@menu/search_menu"&ndash;&gt;-->


        <!--</android.support.v7.widget.SearchView>-->

    </android.support.v7.widget.Toolbar>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

and the toolbar to add to the layout 和添加到布局的工具栏

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:id="@+id/search_toolbar"
    android:background="?attr/colorPrimary">

</android.support.v7.widget.Toolbar>

Here is where I inflate the search_menu 这是我给search_menu充气的地方

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);

            return false;
        }
    });
    return true;
}

Finally this is part of my Adapter class to filter the items in a RecyclerView 最后,这是我的Adapter类的一部分,用于过滤RecyclerView中的项目

 @Override
public Filter getFilter() {
    return locationFilter;
}

private Filter locationFilter = new Filter(){
    @Override
    protected FilterResults performFiltering(CharSequence constraint){
        List<Location> filteredLocationList = new ArrayList<>();

        if(constraint == null || constraint.length() == 0){
            filteredLocationList.addAll(locationListFull);
        } else{
            String filterPattern = constraint.toString().toLowerCase().trim();

            for(Location location : locationListFull){
                if(location.getTitle().toLowerCase().contains(filterPattern)){
                    filteredLocationList.add(location);
                }
                // Add another if statement here if we want to be able to search
                // descriptions as well
            }
        }
        FilterResults results = new FilterResults();
        results.values = filteredLocationList;
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results){
        locationList.clear();
        locationList.addAll((List)results.values);
        notifyDataSetChanged();
    }
};

Also if there's another way to use the SearchView with RecyclerView besides creating a menu for the SearchView, that would be very helpful. 另外,如果除了为SearchView创建菜单之外,还有另一种将SearchView与RecyclerView结合使用的方法,将非常有帮助。 I have tried researching ways to access the SearchView besides through creating a new menu, but haven't found anything useful. 我已经尝试研究通过创建新菜单来访问SearchView的方法,但是还没有发现任何有用的方法。

Prepare the Screen's standard options menu to be displayed. 准备要显示的屏幕标准选项菜单。 This is called right before the menu is shown, every time it is shown. 在显示菜单之前,每次显示菜单时都会调用此方法。 You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents. 您可以使用此方法有效地启用/禁用项目,或者以其他方式动态修改内容。

https://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu) https://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)

You need to override onPrepareOptionsMenu and setup your searchview there instead of doing that in onCreateOptionsMenu 您需要覆盖onPrepareOptionsMenu并在那里设置您的searchview,而不是在onCreateOptionsMenu

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

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