具有可过滤界面的Android RecyclerView

时间:2015-10-01 13:58:47

标签: android android-listview android-recyclerview android-adapter android-filterable

我正在尝试implement RecyclerView Filterable interface Filter来自Toolbar SearcView Widget的{​​{1}}列表。

但我的问题是我无法使用新数据更新我的列表。 notifyDataSetChanged() publishResults方法无效,我不知道原因。

仅更改元素数量。

以下是Recycler Adapter Filterable Interface的实施情况:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ShoppingListHolder> implements Filterable {

    private Context context;
    private ArrayList<ShoppingListModel> shoppingList, filteredShoppingList;
    private ListFilter listFilter;

    public RecyclerAdapter(Context context, ArrayList<ShoppingListModel> shoppingList) {
        this.context = context;
        this.shoppingList = shoppingList;
        this.filteredShoppingList = shoppingList;
    }

    @Override
    public ShoppingListHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shopping_listitem_layout_new, viewGroup, false);
        return new ShoppingListHolder(itemView);
    }

    @Override
    public int getItemCount() {
        return this.filteredShoppingList.size();
    }

    @Override
    public void onBindViewHolder(ShoppingListHolder shoppingListHolder, int position) {
        final ShoppingListModel list = this.filteredShoppingList.get(position);
        shoppingListHolder.titleView.setText(list.getTitle());

        if(list.getAlarmDate() == null || list.getAlarmDate().isEmpty())
            shoppingListHolder.reminderView.setText("--/--/--");
        else
            shoppingListHolder.reminderView.setText(list.getAlarmDate());

        ArrayList<ListItemModel> listItems = DBManager.getShoppingListItems(DBHelper.SHOPPING_LIST_ITEM_PARENT_ID + " = " + list.getId());
        double maxItems = listItems.size();
        double checkedCount = 0;
        shoppingListHolder.itemContainer.removeAllViews();
        for (int i = 0; i < listItems.size(); i++) {
            ListItemModel item = listItems.get(i);
            ShoppingListItemView listItem = new ShoppingListItemView(context, item, i + 1, list.getColor());
            shoppingListHolder.itemContainer.addView(listItem);
            if (item.isChecked() == ListItemModel.ListItemState.Checked.ordinal()) {
                checkedCount++;
            }
        }

        if(checkedCount == 0){
            shoppingListHolder.progressPercentageView.setText("0%");
        }else{
            shoppingListHolder.progressPercentageView.setText((int)(checkedCount / maxItems * 100) + "%");
        }

        shoppingListHolder.progressBar.setMax((int) maxItems);
        shoppingListHolder.progressBar.setProgress((int) checkedCount);

        shoppingListHolder.rippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                Intent shoppingList = new Intent(context, ShoppingListItemActivity.class);
                Bundle extras = new Bundle();
                extras.putLong(ShoppingListModel.SHOPPING_LIST_MODEL_KEY, list.getId());
                shoppingList.putExtras(extras);
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    context.startActivity(shoppingList, ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context).toBundle());
                }else{
                    context.startActivity(shoppingList);
                }
            }
        });
    }

    class ShoppingListHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        RippleView rippleView;
        TextView titleView;
        TextView reminderView;
        ImageView locationPin;
        LinearLayout itemContainer;
        TextView progressPercentageView;
        ProgressBar progressBar;

        public ShoppingListHolder(View itemView) {
            super(itemView);
            rippleView = (RippleView) itemView.findViewById(R.id.card_view_ripple);
            cardView = (CardView) itemView.findViewById(R.id.card_view);
            titleView = (TextView) itemView.findViewById(R.id.title);
            reminderView = (TextView) itemView.findViewById(R.id.reminder);
            locationPin = (ImageView) itemView.findViewById(R.id.location_pin);
            itemContainer = (LinearLayout) itemView.findViewById(R.id.item_container);
            progressPercentageView = (TextView) itemView.findViewById(R.id.progress_percentage_label);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
        }
    }

    @Override
    public Filter getFilter() {
        if (listFilter == null) {
            listFilter = new ListFilter();
        }
        return listFilter;
    }

    private class ListFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            ArrayList<ShoppingListModel> tempList;
            if (constraint != null && constraint.length() > 0) {

                tempList = DBManager.getShoppingList(DBHelper.SHOPPING_LIST_TITLE + " like '%" + constraint + "%' or " +
                                                    DBHelper.SHOPPING_LIST_TAGS + " like '%" + constraint + "%'");

                filterResults.count = tempList.size();
                filterResults.values = tempList;
            } else {
                tempList = DBManager.getShoppingList(null);
                filterResults.count = tempList.size();
                filterResults.values = tempList;
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredShoppingList.clear();
            filteredShoppingList.addAll((ArrayList<ShoppingListModel>) results.values);
            notifyDataSetChanged();
        }
    }
}

提前感谢别人的帮助。

1 个答案:

答案 0 :(得分:0)

我建议您在清除之后添加数组列表而不是执行此操作

/tmp/the_inlinetemplate.txt