本文介绍了Android标签视图片段Recycler视图搜索过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用来自sqlite数据库的数据在Tab视图片段中实施搜索过滤器,但是经过了三天的时间最终没有成功,我遇到了这个,这似乎是个好主意,因为它是android开发的新手. 我试图实现它,但似乎没有用,这意味着搜索菜单项没有膨胀,我也没有得到结果,我不知道我在哪里弄错了,根本没有错误,关于什么是错的任何想法,以及如何使其工作.

I want to implement search filter in Tab view Fragment with data from sqlite database ,after more that three days of being though this and this without success finally i came across this which is seems to be clean idea since am kind new to android development. i tried to implement it but does not seems to work it means search menu item not inflating also i get no result, i do not have idea where do i get t wrong and there is no error at all, any idea on what is wrong and how to make it work.

这是片段代码

public class TabFragment3 extends Fragment implements SearchView.OnQueryTextListener {
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private UsersAdapter adapter;
ArrayList<HashMap<String, String>>  arrayList;
DBController controller = new DBController(getContext());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =inflater.inflate(R.layout.tab_fragment_3, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.usersList);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    controller = new DBController(getContext());
    arrayList = controller.getAllUsers();
    adapter = new UsersAdapter(arrayList,getContext());
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    return view;
      }

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_main, menu);

    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
    MenuItemCompat.setOnActionExpandListener(item,new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
             arrayList = controller.getAllUsers();
           // Do something when collapsed
            adapter.setFilter(arrayList);
            return true; // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
           // Do something when expanded
            return true; // Return true to expand action view
        }
    });
}

@Override
public boolean onQueryTextChange(String query) {
    return false;
}
@Override
public boolean onQueryTextSubmit(String newText) {
 // Here is where we are going to implement the filter logic
    newText = newText.toLowerCase();
    ArrayList<HashMap<String, String>> userlist = controller.getAllUsers();
    ArrayList<HashMap<String, String>> newList= new ArrayList<>();
    for(HashMap<String, String> entry : userlist){
        String name2= entry.get("number").toLowerCase();
        if(name2.contains(newText)){
            newList.add(entry);
        }}
    adapter.setFilter(newList);
    return true;
}

}

 ///  adapter class

 public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
ArrayList<HashMap<String, String>> mDataSet= new ArrayList<HashMap<String, String>> ();
Context ctx;
public UsersAdapter(ArrayList<HashMap<String, String>> mDataSet,Context ctx) {
    this.mDataSet = mDataSet;
    this.ctx=ctx;
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
    UserViewHolder userViewHolder = new UserViewHolder(v,mDataSet,ctx);
    return userViewHolder;
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
    holder.name_entry.setText(mDataSet.get(position).get("userId"));
    holder.email_entry.setText(mDataSet.get(position).get("userName"));
    holder.icon_entry.setText(""+mDataSet.get(position).get("number"));
}
@Override
public int getItemCount() {
    return mDataSet.size();
}

public static class UserViewHolder extends RecyclerView.ViewHolder  {
    CardView cardView;
    TextView name_entry, email_entry, icon_entry;
    Context ctx;
    ArrayList<HashMap<String, String>> mDataSet= new ArrayList<HashMap<String, String>> ();
    public UserViewHolder(View itemView, ArrayList<HashMap<String, String>> mDataSet,Context ctx) {
        super(itemView);
        this.mDataSet=mDataSet;
        this.ctx= ctx;
        cardView = (CardView) itemView.findViewById(R.id.user_layout);
        name_entry = (TextView) itemView.findViewById(R.id.name_entry);
        email_entry = (TextView) itemView.findViewById(R.id.email_entry);
        icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
    }
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
public void setFilter(ArrayList<HashMap<String, String>> newList){
    mDataSet= new ArrayList<HashMap<String, String>>();
    mDataSet.addAll(newList);
    notifyDataSetChanged();
}

}

///主要

public class MainActivity extends AppCompatActivity  implements TabLayout.OnTabSelectedListener,SearchView.OnQueryTextListener  {
private ViewPager viewPager;
PagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}

public boolean onQueryTextSubmit(String query)
{ return false;
 }
@Override
public boolean onQueryTextChange(String newText) {

  return false;
        }

}

推荐答案

  1. 在TabFragment3的onCreateView()方法中添加setHasOptionsMenu(true).它告诉主机活动,您的片段具有要添加的菜单选项.

  1. Add setHasOptionsMenu(true) in your TabFragment3's onCreateView() method. It tells the host activity that your fragment has menu options that it wants to add.

还添加super.onCreateOptionsMenu(menu,inflater) ..

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_main, menu); super.onCreateOptionsMenu(menu,inflater); }

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_main, menu); super.onCreateOptionsMenu(menu,inflater); }

在MainActivity的onOptionsItemSelected(MenuItem item) 返回false 内,因为菜单项操作将在TabFragment3的onOptionsItemSelected(MenuItem item)中进行处理.

Inside the MainActivity's onOptionsItemSelected(MenuItem item) return false, as the Menu item action would be hanlded in TabFragment3's onOptionsItemSelected(MenuItem item).

这篇关于Android标签视图片段Recycler视图搜索过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 19:37