android tab view fragment Recycler view search filter

夙愿已清 提交于 2019-12-23 04:29:47

问题


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.

here is the Fragment code

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();
}

}

/// main

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:


  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.

  2. Also add super.onCreateOptionsMenu(menu,inflater)..

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

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



来源:https://stackoverflow.com/questions/43319756/android-tab-view-fragment-recycler-view-search-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!