通过在Action Bar中编写搜索查询来搜索(过滤ListView)片段

时间:2014-12-03 20:52:06

标签: android listview android-fragments android-listview

是否可以在Fragments中实现搜索(过滤ListView),它会通过文本过滤ListView,立即以相同的布局写入Action Bar中的搜索查询?或者搜索是否需要活动类,而且无法处理它? 示例:像这些家伙一样(我不确定,如果他们使用了Acitivity或Fragment) enter image description here

当用户向搜索栏添加一些文字时 enter image description here 编辑:我做错了什么?

public class AllLists extends Fragment /*implements SearchView.OnQueryTextListener*/{

private TypedArray navMenuIcons;
private Context context;
private int position = 0;
private String location;
private List<Item> items = new ArrayList<Item>();
private ListView listView;
private CustomListAdapter adapter;

public AllLists(Context context) {
    this.context = context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.alllists, container, false);
    View header = inflater.inflate(R.layout.list_header, null);
  //  View list_view = inflater.inflate(R.layout.list_item, null);
    ImageView image = (ImageView) header.findViewById(R.id.small_icon);
    header.setClickable(false);
   // ImageView logo = (ImageView) list_view.findViewById(R.id.image);
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
    Bundle bundle = getArguments();
    position = bundle.getInt("position");
    location = bundle.getString("location");
    image.setImageDrawable(navMenuIcons.getDrawable(position));
    DatabaseHandler db = new DatabaseHandler(context);
    items = db.getAllItems(location);
    listView = (ListView) rootView.findViewById(R.id.list);
    adapter = new CustomListAdapter(context, items);
    listView.setAdapter(adapter);
    listView.addHeaderView(header);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (location.equals("accommodation") || location.equals("eat") || location.equals("events")
                    || location.equals("entertainment") || location.equals("places") || location.equals("cinema")) {
                Intent i = new Intent(context, ItemScreen.class);
                i.putExtra("position", position - 1);
                i.putExtra("location", location);
                startActivity(i);
            }
        }
    });
    setHasOptionsMenu(true);
    return rootView;
}

/*SearchView.OnQueryTextListener(new SearchView.OnQueryTextListener){

}*/

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.main, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }
        /*public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

            adapter.getFilter().filter(cs);
        }*/
        @Override
        public boolean onQueryTextChange(String s) {
            adapter.getFilter().filter(s);
            return false;
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

您的目标是将搜索查询发送到包含列表的片段......为此,您需要在活动与其包含的片段之间进行通信 ..以便用户输入包含列表的搜索查询片段将相应地过滤...

第1步:在片段中定义一个公共方法,该方法将被附加到的活动访问。

Class ExampleFragment {

public void refreshList(String search){
// code to refresh the list view
}

第2步:通过以下方法找到附加到您活动的片段

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

第3步:使用片段访问该片段的公共方法,并刷新列表视图,例如:

 String searchQuery = "apple";
 fragment.refreshList(searchQuery)

}