我想实现以下屏幕:
正如你在这里看到的那样,SearchView在包含三个tab的活动中。每个标签都是使用片段实现的,片段又包含listview。我在包含搜索视图的活动中有View Viewr。查看Pager通过片段填充FragmentStatePagerAdapter.I想要使用搜索视图过滤活动选项卡的列表视图。
包含SearchView的活动代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_friend_list_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(getApplicationContext(), "Search button clicked", Toast.LENGTH_SHORT).show();
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) FriendsListActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (item != null) {
searchView = (SearchView) item.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(FriendsListActivity.this.getComponentName()));
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_friend_list_activity.java
<?xml version="1.0" encoding="utf-8"?>
<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/search"
android:orderInCategory="100"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
我正在使用上面提到的代码。我想我在这里做错了。请帮助我在活动中使用searchview过滤片段中包含的listview。
编辑工作代码:
经过大量研究后,我设法解决了我的问题。我正在添加我的代码供将来参考:
1。包含SearchView的Activity代码(在我的情况下为FriendsListActivity.java)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_friend_list_activity, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if (null != searchView) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
Log.e("Text",newText);
return false;
}
public boolean onQueryTextSubmit(String query) {
//Here u can get the value "query" which is entered in the search box.
// Log.e("Query",query);
PagerAdapter pagerAdapter = (PagerAdapter) viewPager.getAdapter();
for(int i = 0; i < pagerAdapter.getCount(); i++) {
Fragment viewPagerFragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, i);
if(viewPagerFragment != null && viewPagerFragment.isAdded()) {
if (viewPagerFragment instanceof ChatFragment){
ChatFragment chatFragment = (ChatFragment) viewPagerFragment;
if (chatFragment != null){
chatFragment.beginSearch(query); // Calling the method beginSearch of ChatFragment
}
}else if (viewPagerFragment instanceof GroupsFragment){
GroupsFragment groupsFragment = (GroupsFragment) viewPagerFragment;
if (groupsFragment != null){
groupsFragment.beginSearch(query); // Calling the method beginSearch of GroupsFragment
}
}else if (viewPagerFragment instanceof ContactsFragment){
ContactsFragment contactsFragment = (ContactsFragment) viewPagerFragment;
if (contactsFragment != null){
contactsFragment.beginSearch(query); // Calling the method beginSearch of ContactsFragment
}
}
}
}
return false;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
我已使用this链接查找当前在视图寻呼机中显示的片段。
您可以在上面提到的代码中看到我使用Fragment中的方法名称beginSearch()将查询字符串从activity的searchview传递给fragment。
2.片段内的方法(在我的案例中是ContactsFragment.java)
public void beginSearch(String query) {
Log.e("QueryFragment", query);
adapter_contacts.getFilter().filter(query);
}
这里的adapter_contacts是填充名为ContactsFragment的Fragment的列表视图的适配器。
3.适配器代码(在我的情况下是Adapter_Contacts.java)
public class Adapter_Contacts extends BaseAdapter implements Filterable {
private Context context;
private List<Bean_Contacts> listContacts;
private LayoutInflater inflater;
private ApiConfiguration apiConfiguration;
List<Bean_Contacts> mStringFilterList;
ValueFilter valueFilter;
public Adapter_Contacts(Context context, List<Bean_Contacts> listContacts) {
this.context = context;
this.listContacts = listContacts;
mStringFilterList = listContacts;
}
@Override
public int getCount() {
return listContacts.size();
}
@Override
public Object getItem(int i) {
return listContacts.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
view = inflater.inflate(R.layout.feed_item_contact_list, null);
//Getting views
ImageView img = (ImageView) view.findViewById(R.id.imgContact);
TextView txtName = (TextView) view.findViewById(R.id.txtNam);
TextView txtStatus = (TextView) view.findViewById(R.id.stats);
Bean_Contacts bean_contacts = listContacts.get(i);
String name = bean_contacts.getName();
Log.e("NameAdapter", name);
String url = bean_contacts.getUrl();
Log.e("URLAdapter", url);
String extension = bean_contacts.getExtension();
String status = bean_contacts.getStatus();
apiConfiguration = new ApiConfiguration();
String api = apiConfiguration.getApi();
String absoluteURL = api + "/" + url + "." + extension;
Log.e("AbsoluteURLAdapter", absoluteURL);
Picasso.with(context).load(absoluteURL).error(R.drawable.default_avatar).into(img); //Loading image into the circular Image view using Picasso
txtName.setText(name);
txtStatus.setText(status);
return view;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String str = constraint.toString().toUpperCase();
Log.e("constraint", str);
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Bean_Contacts> filterList = new ArrayList<Bean_Contacts>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Bean_Contacts bean_contacts = mStringFilterList.get(i);
filterList.add(bean_contacts);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
listContacts = (ArrayList<Bean_Contacts>) results.values;
notifyDataSetChanged();
}
}
}
这里我使用的是Filterable接口和Filter类。这就是我在Activity中使用单个SearchView实现片段搜索内容的方法。请注意My Activity包含SearchView,3个选项卡和ViewPager.ViewPager通过片段填充使用FragmentStatePagerAdapter。