您好,我想通过 SearchView
小部件在我的应用中搜索用户.当 SearchWidget
处于活动状态时,我想在相同 XML
布局的 ViewPager
上方的 ListView
中显示建议的用户名.我使用了以下 xml
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>
并且在代码中我可见 ListView
以显示用户.
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;
}
它不显示 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;
}
它显示以下输出
但我希望 ListView
高于 ViewPager
.提前致谢.
But i want ListView
above ViewPager
. Thanks in advance.
如果您希望您的 ListView 实时显示用户将在 searchView 中建议的结果 - 请在您的 Adapter 中使用 Filter.
If you want your ListView to show results in real time which user will suggest in searchView - use Filter in your 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.
这篇关于Android 在 ViewPager 上方显示 SearchView 结果 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!