问题
Hi i want to search users in my app via SearchView
widget. I want to show suggested users name in a ListView
above a ViewPager
of same XML
layout when SearchWidget
is active.
I used following xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/searched_friends_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:visibility="gone"
android:listSelector="@drawable/list_row_selector" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
And in code i visibled ListView
to show users.
@Override
public boolean onQueryTextChange(String newText) {
friendListView.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(newText)) {
friendListView.clearTextFilter();
} else {
friendListView.setFilterText(newText.toString());
}
return true;
}
it doesn't show ListView
but when i add following code
@Override
public boolean onQueryTextChange(String newText) {
friendListView.setVisibility(View.VISIBLE);
viewPager.setVisibility(View.GONE);
if (TextUtils.isEmpty(newText)) {
friendListView.clearTextFilter();
} else {
friendListView.setFilterText(newText.toString());
}
return true;
}
It shows following output
But i want ListView
above ViewPager
. Thanks in advance.
回答1:
If you want your ListView to show results in real time which user will suggest in searchView - use Filter in your Adapter.
Add to adapter:
ArrayList<full_data> adapterDataList = new ArrayList<full_data>();
ArrayList<filtered_data> mFilteredDataList = new ArrayList<filtered_data>(listOR);
Filter mFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence searchTerm) {
Locale locale = Locale.getDefault();
FilterResults results = new FilterResults();
if ((searchTerm null) || (searchTerm.length() 0)) {
ArrayList<String> searchList = new ArrayList<String>(adapterDataList);
results.values = searchList;
results.count = searchList.size();
} else {
final String searchTermString = searchTerm.toString().toLowerCase(locale);
final String[] words = searchTermString.split(" ");
final int wordCount = words.length;
final ArrayList<String> newValues = new ArrayList<String>();
for (final String value : adapterDataList) {
if (value == null) {
continue;
}
final String valueText = value.toLowerCase(locale);
for (int k = 0; k < wordCount; k++) {
if (valueText.contains(words[k])) {
newValues.add(value);
break;
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilteredDataList = (ArrayList<String>)results.values;
notifyDataSetChanged();
}
};
public Filter getFilter(){ return mFilter; }
to SearchView's TextChangedListener:
@Override
public void afterTextChanged(Editable searchText) {
mAdapter.getFilter().filter(searchText);
}
I think it should work fine.
来源:https://stackoverflow.com/questions/26500864/android-show-searchview-result-listview-above-viewpager