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

时间:2018-04-15 21:30:03

标签: android android-layout listview search-suggestion

我已经使用了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" />

我的活动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

请指导我,谢谢你...

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

问题实际上是由于MaterialSearchView是Coordinator Layout中的子项而导致的建议列表未完全显示,可能是MaterialSearchView的错误。 是的,将协调器布局替换为其他任何布局都可以解决此问题。

但是,如果要保留“协调器布局”,则可以使用“相对布局”作为根布局,并在“协调者”布局为第一个子项的情况下将MaterialSearchView添加为“相对”布局中的最后一个子项。这应该使建议列表可滚动,这将使建议列表按照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>
相关问题