为什么RecyclerView onClick方法不起作用?

时间:2018-06-26 08:56:42

标签: android android-recyclerview

实际上,我已经确定,当我按下recyclerView中的某些项目时,它们会通过更改背景来突出显示。

但是问题出在我尝试在活动中使用adapter.onClick方法时,我的意思是如果我不使用适配器中更改颜色的方法,则onClick在活动中有效,但是当我在不起作用的适配器中使用时, t。

在活动中,我只是想通过单击来自recyclerView的项目来做吐司进行测试,但是如果我有更改所选项目的颜色背景的方法,那将不起作用。

这是我的适配器代码:

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

//CHANGING COLOR ON CLICK AND BACK TO MAIN COLOR IF CLICKED AGAIN!

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.setSelected(!item.isSelected());
                if(position % 2 == 0 ) {
                    holder.itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : Color.parseColor("#17e1a0"));
                }else if(position % 2 == 1){
                    holder.itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : 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);
    }

}

这是我尝试烤面包时来自“活动”的代码:

adapterVarianti.setOnItemClickListener(new AdapterVarianti.OnItemClickListener() {
    @Override
    public void onItemClick(int position) {
        if(tipo.toString().equals("CON ") || tipo.toString().equals("PIU' ") || tipo.toString().equals("MENO ") || tipo.toString().equals("SENZA ")) {
            tipo.append(filteredVariable.get(position).getDeskT());
            Toast.makeText(pterm.this,tipo,Toast.LENGTH_LONG).show();
            tipo = new StringBuilder();
        }
    }
});

3 个答案:

答案 0 :(得分:1)

  

为什么RecyclerView onClick方法不起作用?

主要是因为您要在onBindViewHolder中用以下命令覆盖它:

holder.itemView.setOnClickListener

成为二传手,最后获胜。您的情况下最简单的解决方案是从那里调用onItemClick

答案 1 :(得分:1)

因为您在onBindViewHolderExampleViewHolder中为itemView设置了两次onclick侦听器 因此请删除onBindViewHolder

中的以下代码
holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.setSelected(!item.isSelected());
                if(position % 2 == 0 ) {
                    holder.itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : Color.parseColor("#17e1a0"));
                }else if(position % 2 == 1){
                    holder.itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : Color.parseColor("#7EC0EE"));
                }
            }
        });

并更新您的ExampleViewHolder构造函数

 itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                final VariantiConstructor item = mFilteredList.get(position);
                item.setSelected(!item.isSelected());
                if(position % 2 == 0 ) {
                    itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : Color.parseColor("#17e1a0"));
                }else if(position % 2 == 1){
                    itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : Color.parseColor("#7EC0EE"));
                }
                if(listener != null){
                    if(position != RecyclerView.NO_POSITION){
                        listener.onItemClick(position);
                    }
                }
            }
        });

答案 2 :(得分:0)

问题是因为您在ViewHolderonBindViewHolder中设置了onClickListener。只需评论以下几行:

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            item.setSelected(!item.isSelected());
            if(position % 2 == 0 ) {
                holder.itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : Color.parseColor("#17e1a0"));
            }else if(position % 2 == 1){
                holder.itemView.setBackgroundColor(item.isSelected() ?  Color.parseColor("#f4c542") : Color.parseColor("#7EC0EE"));
            }
        }
    });

现在应该可以使用