我正在创建一个带过滤器的ListView。问题是,过滤器正在工作,但我需要按搜索按钮,以便可以过滤列表。我想避免这种情况,并在输入信件时自动过滤。
以下是活动实施:
private void setupSearchView() {
filter = navAdapter.getFilter();
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
search.setSubmitButtonEnabled(false);
search.setOnQueryTextListener(this);
search.setIconified(true);
int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchEditText = (EditText) search.findViewById(searchSrcTextId);
searchEditText.setTextColor(Color.WHITE);
searchEditText.setHintTextColor(Color.LTGRAY);
}
@Override
public boolean onQueryTextSubmit(String query) {
filter.filter(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
适配器:
@Override
public Filter getFilter() {
/**
* A filter object which will
* filter message key
* */
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
list = (List<NavigationDrawerListModel>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values. Only filtered values will be shown on the list
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation for publishing
List<NavigationDrawerListModel> FilteredArrList = new ArrayList<NavigationDrawerListModel>();
if (mList == null) {
mList = new ArrayList<NavigationDrawerListModel>(list); // saves the original data in mOriginalValues
}
if (mListItem == null) {
mListItem = new ArrayList<String>();
for (NavigationDrawerListModel recipe : mList) {
mListItem.add(recipe.getTitle());
}
}
/**
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
**/
if (constraint == null || constraint.length() == 0) {
/* CONTRACT FOR IMPLEMENTING FILTER : set the Original values to result which will be returned for publishing */
results.count = mList.size();
results.values = mList;
} else {
/* Do the filtering */
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mListItem.size(); i++) {
String data = mListItem.get(i);
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(mList.get(i));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
和XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<!-- Toolbar -->
<include layout="@layout/toolbar" />
<!-- content -->
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:id="@+id/drawerLayout_test"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Our Content"
android:layout_height="wrap_content" />
</RelativeLayout>
<!-- nav drawer -->
<LinearLayout
android:layout_gravity="start"
android:orientation="vertical"
android:background="@color/black"
android:layout_width="280dp"
android:layout_height="match_parent"
>
<android.widget.SearchView
android:id="@+id/searchViewHomePage_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:iconifiedByDefault="false"
android:queryHint="@string/abc_searchview_description_search"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ListView
android:id="@+id/drawer_list_items_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="0.1dp">
<ListView
android:id="@+id/drawer_list_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
答案 0 :(得分:0)
使用SearchView#setOnQueryTextListener
并使用boolean onQueryTextChange(String newText)
OnQueryTextListener
方法进行过滤。因此,在您的情况下,将filter.filter(query);
从onQueryTextSubmit
移至onQueryTextChange
。