如何在没有getItemCount返回零的情况下清除filterList?

时间:2017-01-31 07:26:00

标签: android android-recyclerview android-adapter

我在RecyclerView适配器文件中有一个filterList,用于过滤CardView项目列表。当我单击action_search MenuItem以在列表上启动SearchView时,将清除filterList并返回搜索。我有一个EmptyRecyclerView类,它运行一个checkIfEmpty(),当列表中没有卡时(getItemCount()== 0),它返回一个默认的CardView。我的问题是每次我在适配器中运行filter()方法时,filterList被清除并且getItemCount()== 0并且View翻转到默认的CardView。 filterList.clear()仅暂时使用,因为最终的filterList会立即返回SearchView中的匹配项。当filterList.clear()使getItemCount == 0时,如何避免View翻转?

主要Activity.java

...
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.cardview_menu, menu);
    MenuItem item = menu.findItem(R.id.action_search);

    if (adapter == null || adapter.getItemCount() == 0) {
        item.setVisible(false);
    } else {
        item.setVisible(true);
        // Get the SearchView and set the searchable configuration
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint(" Search here...");
    }
    return super.onCreateOptionsMenu(menu);
} 

适配器

public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

   private List<ListItem> listItems, filterList;
   private Context mContext;
   public boolean mFilterSearch = false;  \\ *** added
   ...
   public MyRecylerAdapter(Context context, List<ListItem> listItems) {
    this.listItems = listItems;
    this.mContext = context;
    this.filterList = new ArrayList<>();
    this.filterList.addAll(this.listItems);
}

    public void clear() {
        while (getItemCount() > 0) {
        remove(getItem(0));
    }
}

    @Override
    public int getItemCount() {
        return (null!= filterList ? filterList.size() : 0);
    }

    public void filter(final String text) {

        new Thread(new Runnable() {
            @Override
            public void run() {

                mFilterSearch = true;  \\ *** added
                filterList.clear();  

                // If there is no search value, then add all original list items to filter list
                if (TextUtils.isEmpty(text)) {
                    filterList.addAll(listItems);
                }
                else {

                    // Iterate in the original List and add it to filter list...
                    for (ListItem item : listItems) {  
                    ...  
                    mFilterSearch = false; \\ *** added

EmptyRecyclerView

public class EmptyRecyclerView extends RecyclerView {

private View emptyView;
private MyRecyclerAdapter adapter;
...
void checkIfEmpty() {
    if (adapter.mFilterSearch = false && emptyView != null && getAdapter() != null) { \\ *** added "adapter.mFilterSearch &&"
        final boolean emptyViewVisible =
                getAdapter().getItemCount() == 0;
        emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
        setVisibility(emptyViewVisible ? GONE : VISIBLE);
    }
}

0 个答案:

没有答案