Android:如何在RecyclerView列表中维护CardViews的视图? [英] Android: how do I maintain the View for CardViews in a RecyclerView list?

查看:54
本文介绍了Android:如何在RecyclerView列表中维护CardViews的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView列表,其中显示了CardViews的垂直列表. AppBar上有一个action_search MenuItem,可在单击该菜单项时处理SearchView方法. SearchView运行一种过滤器方法,该方法可以搜索CardView数据.

I have a RecyclerView list that shows a vertical list of CardViews. The AppBar has an action_search MenuItem that handles a SearchView method when clicked on. The SearchView runs a filter method that allows the CardView data to be searched.

我设置了filterList来保存从搜索结果中过滤出的CardViews. filter()方法在执行搜索查询之前清除filterList.问题在于filterList.clear()破坏了现有RecyclerView CardView的视图,UI还原为空白的RecyclerView布局.

I set up a filterList to hold filtered CardViews from the search results. The filter() method clears the filterList before conducting the search query. The problem is that the filterList.clear() destroys the View of the existing RecyclerView CardViews and the UI reverts to the blank RecyclerView layout.

如果我后退应用程序外的按钮,然后重新启动应用程序,则RecyclerView会正确加载CardViews,并且当我单击Search MenuItem时,便可以正确搜索;即视图"会动态且正确地更新,以仅显示与搜索"文本查询匹配的CardView.我在这里想念什么?

If I back button out of the app and then re-start the app, the RecyclerView loads the CardViews correctly and when I click on the Search MenuItem I am then able to search correctly; that is the View dynamically and correctly updates to only show those CardViews that match the Search text querty. What am I missing here?

MainActivity.java

public class MainActivity extends AppCompatActivity implements
    RecyclerItemClickListener {

    private MyRecylerAdapter adapter;
    ...
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.cardview_menu, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView item);            
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                adapter.filter(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.filter(newText);
                return true;
            }
        });
        return;
}

 Adapter.java

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

     private List<ListItem> listItems, filterList;
     private Context mContext;

     public MyRecylerAdapter(Context context, List<ListItem> listItems) {
         this.listItems = listItems;
         this.mContext = context;
         this.filterList = new ArrayList<>();
         // Copy the original list to the filter list to be used for setting row
         // values when returning search data items.
         this.filterList.addAll(this.listItems);
     }

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

     public void filter(final String text) {

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

            filterList.clear();

            if(TextUtils.isEmpty(text)) {
                filterList.addAll(listItems);
            } else {                    
                for (ListItem item : listItems) {
                    if (item.getTodo().toLowerCase().contains(text.toLowerCase())) {                        
                        filterList.add(item);
                    }
                }
            }

            // Set on UI Thread
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {                        
                    notifyDataSetChanged();
                }
            });
        }
    }).start();
} 

}

推荐答案

尝试此代码.在适配器中设置此方法

try this code.. set this method in adapter

public void setFilter(ArrayList<ListItem> listItem) {
        listItems = new ArrayList<>();
        listItems.addAll(listItem);
        notifyDataSetChanged();
    }

并在onQueryTextChange()

final ArrayList<ListItem> filteredModelList = filter(listItems, newText);
adapter.setFilter(filteredModelList);

这是过滤方法.

private ArrayList<ListItem> filter(ArrayList<ListItem> models, String query) {
        query = query.toLowerCase();

        final ArrayList<ListItem> filteredModelList = new ArrayList<>();
        for (ListItem model : models) {
            final String text = model.getTodo().toLowerCase();
            if (text.contains(query)) {
                filteredModelList.add(model);
            }
        }

        return filteredModelList;
    }

,然后从适配器构造函数中删除最后两行.使用此代码,您不必创建第二个数组列表对象进行过滤,因此您无需清除arraylist对象.当您在searchview中写入或清除文本时,它会很好地工作.

and remove last two lines from your adapter constructor. with this code you dont have to create second array list object for filter, so you dont need to clear your arraylist object.. it works good when you write or clear text in searchview.

这篇关于Android:如何在RecyclerView列表中维护CardViews的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆