搜索视图中的建议(高度)

suggestion in search view (height)

我用过MaterialSearchView库,关键是我在使用这个库之前Activity使用了导航抽屉,所以我不得不使用两层菜单XML. 不知道对不对我已经采样了我以前的代码:

这用于导航抽屉菜单项:

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_SellPaper"
        android:icon="@drawable/ic_seller"
        android:title="@string/SellMyPaper" />
    <item
        android:id="@+id/nav_sendPaper"
        android:icon="@drawable/ic_input_black_24dp"
        android:title="@string/SendPaperMenu" />

    <item
        android:id="@+id/nav_MyDownloads"
        android:icon="@drawable/ic_lib"
        android:title="@string/MyLibMenu" />
    <item
        android:id="@+id/nav_fav"
        android:icon="@drawable/ic_favorite"
        android:title="@string/MyFav" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_account_box_black_24dp"
        android:title="@string/UserAccMenu" />
    <item
        android:id="@+id/nav_share"
        android:icon="@drawable/ic_share_black"
        android:title="@string/ShareMenu" />
    <item
    android:id="@+id/nav_slideshow"
    android:icon="@drawable/ic_slideshow"
    android:title="@string/LearningMenu" />
    <item
    android:id="@+id/nav_logout"
    android:icon="@drawable/ic_exit_to_app"
    android:title="@string/LogoutMenu" />
</group>

<item android:title="@string/ConnectUsMenu">
    <menu>
        <item
            android:id="@+id/send_tel"
            android:icon="@drawable/ic_send_black_24dp"
            android:title="@string/TelegramMe" />
        <item
            android:id="@+id/send_mail"
            android:icon="@drawable/ic_email_black_24dp"
            android:title="@string/EmailME" />
    </menu>
</item>

这是 搜索菜单 项目:

<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_action_action_search"
    android:orderInCategory="100"
    android:title="@string/abc_search_hint"
    app:showAsAction="always" />

Appbar布局中我使用了以下代码:

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        app:titleTextColor="@color/TextView"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>


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

我的 Activity java 代码 包含以下内容:

        public class MenuActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, View.OnClickListener, MaterialSearchView.OnQueryTextListener, MaterialSearchView.SearchViewListener {
     MaterialSearchView searchView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu);
            searchView = findViewById(R.id.search_view);
            searchView.setVisibility(GONE);
            searchView.setOnQueryTextListener(this);
            searchView.setOnSearchViewListener(this);
...
 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, SearchURL, new Listener<String>() {

            @Override
            public void onResponse(String response) {

                try {
                    //this is my output array
                    ArrayList<String> arrayList = new ArrayList<>();

                    //lvl1
                    JSONArray jsonarray = new JSONArray(response);
                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        //lvl2
                        JSONArray catArray  = jsonobject.getJSONArray("Category");
                        for(int j = 0; j < catArray.length(); j++){

                            //lvl3
                            if(!arrayList.contains(catArray.getString(j)) && !catArray.isNull(j) ){

                                arrayList.add(catArray.getString(j));

                            }
                        }

                    }

                    SharedPreferences prefs=PreferenceManager.
                            getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor edit=prefs.edit();
                    Set<String> set = new HashSet<String>(arrayList);
                    edit.putStringSet("CAT", set);
                    edit.apply();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }

        });

        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);

 Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
     DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);

        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
....
}

   @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_Sell) {

     .........
        }


        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    ...
     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);

        MenuItem item = menu.findItem(R.id.action_search);
        searchView.setMenuItem(item);

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (matches != null && matches.size() > 0) {
                String searchWrd = matches.get(0);
                if (!TextUtils.isEmpty(searchWrd)) {
                    searchView.setQuery(searchWrd, false);
                }
            }

            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    @Override
    public void onSearchViewShown() {
        searchView.setVisibility(View.VISIBLE);

        SharedPreferences prefs = PreferenceManager.
                getDefaultSharedPreferences(getApplicationContext());
        Set<String> set = prefs.getStringSet("CAT", null);
        List<String> sample = null;
        String[] values = new String[0];
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            sample = new ArrayList<String>(Objects.requireNonNull(set));

            values = new String[Objects.requireNonNull(sample).size()];

            for (int i = 0; i < sample.size(); i++) {
                values[i] = sample.get(i);
            }
        }

        searchView.setSuggestions( getResources().getStringArray(R.array.query_suggestions)  );


    }

    @Override
    public void onSearchViewClosed() {
        searchView.setVisibility(View.GONE);


    }

使用这个方法后,我遇到了一个问题,给我的搜索建议不超过一个,我想看到更多的建议。

下图显示我有3个建议,但我只能看到一个建议:

我看到的:

What I see

我想看的:

What I want to see

请指导我谢谢...

我建议您将高度设置为android:layout_height="?attr/actionBarSize"。这就是我在几乎所有情况下所做的。

这个问题实际上是由于 MaterialSearchView 是 CoordinatorLayout 中的一个子项,建议列表没有完全显示,可能是 MaterialSearchView 的一个错误。 是的,用任何其他布局替换协调器布局应该可以解决问题。

但是,如果您想保留 Coordinator Layout,那么作为一种解决方法,请使用 Relative Layout 作为根布局,并将 MaterialSearchView 添加为 Relative 布局中的最后一个子元素,而 Coordinator 布局为第一个子元素。这应该使建议列表可滚动,这将使建议列表完全显示 here

参见下面的示例:

<?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:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="false"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

<-your rest of the code->

    </android.support.design.widget.CoordinatorLayout>

    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="pin"
        app:searchSuggestionIcon="@drawable/ic_history_grey_600_24dp" />
</RelativeLayout>