如何通过ID过滤一个recyclerView?

时间:2018-06-26 10:27:38

标签: android android-recyclerview

我在过滤我的recyclerView时遇到一些问题,我还在适配器中使用过滤器在searchView中进行搜索,但是我也有一种方法可以在recyclerView中加载,仅创建具有特定ID的商品。

在示例中,我有一个菜单,可以将其设置为MenuID 1,2,3,4等。而我还有一些按钮,这些按钮可以具有MenuID 1,2,3,4,这些按钮指定某个按钮属于哪个菜单。因此,如果我打开“活动”并选择菜单1,则必须仅显示属于菜单1的按钮,等等。

我已经完成了此操作,但是我在Activity上使用了它,但这不是一个好习惯,因此我将其移至我的recyclerView适配器并在适配器中进行过滤。

这是我在“活动”中进行过滤的方法:

public void FilterVariable(){


    filteredVariable = new ArrayList<>();

    // i check for all the items from the constructor if there are any item 
    // that have the same MenuID which i get with .getMenu equals to positionMenu 
    // positionMenu is an int value that i get when i press on a Menu and it's 
    // give me it's ID (onCreate by default selected menu is menu 1)

    for (VariantiConstructor varianti : variantiConstructors) {
    String data = varianti.getMenu();
    String[] items = data.split(","+positionMenu+",");


    try {
        if(items[0].equals(data)){
            //
        }else{
            filteredVariable.add(varianti);
        }
    }catch (Exception e){
        //
    }


    }
}

这是我的适配器代码:

public class AdapterVarianti extends RecyclerView.Adapter<AdapterVarianti.ExampleViewHolder>  implements Filterable {


    private ArrayList<VariantiConstructor> variantiConstructors;
    private ArrayList<VariantiConstructor> mFilteredList;
    private OnItemClickListener mListener;

    public interface  OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }

    @NonNull
    @Override
    public AdapterVarianti.ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.varianti_recycler,parent,false);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        v.setLayoutParams(lp);
        return new AdapterVarianti.ExampleViewHolder(v, mListener);
    }

    AdapterVarianti(ArrayList<VariantiConstructor> exampleList){
        variantiConstructors = exampleList;
        mFilteredList = variantiConstructors;
    }

    @Override
    public void onBindViewHolder(@NonNull final AdapterVarianti.ExampleViewHolder holder, final int position) {
        final VariantiConstructor item = mFilteredList.get(position);

        holder.desc.setText(item.getDeskT());
        holder.prepiu.setText(String.valueOf(item.getPrice()));
        holder.premeno.setText(String.valueOf(item.getPacq()));

        if(position % 2 == 0 ){
            holder.itemView.setBackgroundColor(Color.parseColor("#17e1a0"));

        }else if(position % 2 == 1){
            holder.itemView.setBackgroundColor(Color.parseColor("#7EC0EE"));
        }

    }


    @Override
    public Filter getFilter() {

        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {

                String charString = charSequence.toString();

                if (charString.isEmpty()) {

                    mFilteredList = variantiConstructors;
                } else {

                    ArrayList<VariantiConstructor> filteredList = new ArrayList<>();

                    for (VariantiConstructor androidVersion : variantiConstructors) {

                        if (androidVersion.getDeskT().toLowerCase().contains(charString)) {

                            filteredList.add(androidVersion);
                        }
                    }

                    mFilteredList = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilteredList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                mFilteredList = (ArrayList<VariantiConstructor>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

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

    public class ExampleViewHolder extends RecyclerView.ViewHolder {

        public TextView desc;
        public TextView prepiu;
        public TextView premeno;

        ExampleViewHolder(View itemView, final OnItemClickListener listener) {
            super(itemView);

            desc = itemView.findViewById(R.id.Desc);
            prepiu = itemView.findViewById(R.id.PrePiu);
            premeno = itemView.findViewById(R.id.PreMeno);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null){
                        int position = getAdapterPosition();
                        if(position != RecyclerView.NO_POSITION){
                            listener.onItemClick(position);
                        }
                    }
                }
            });


        }
    }

    public void removeItem(int position) {
        variantiConstructors.remove(position);
        notifyItemRemoved(position);
    }

}

0 个答案:

没有答案