如何在RecyclerView中更改特定项目onClick的背景? [英] How do I change the background of a specific item onClick in a RecyclerView?

查看:66
本文介绍了如何在RecyclerView中更改特定项目onClick的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在RecyclerView中创建一个选定的项目以在单击时更改其背景,但是一旦单击RecyclerView中的另一个项目,则该项目的背景将被更改,并且先前单击的项目将恢复为原始状态.

Im trying to make a selected item in the RecyclerView to change it's background when it is clicked, but once the other item in the RecyclerView is clicked, that item's background will be changed and the previously clicked item will change back to original.

我只能设法使该项目在第二次单击时变回原始(白色背景).

I only manage to make the item change back to orignal(white background) when it is clicked the second time.

请帮忙吗?

这是我的RecyclerView适配器

public class CharityListAdapter extends RecyclerView.Adapter<CharityListAdapter.CharityListViewHolder> {
    String charityData[], descriptionData[];
    int images[];
    Context context;

    public CharityListAdapter(Context ct, String charity[], String description[], int image[]) {
        context = ct;
        charityData = charity;
        descriptionData= description;
        images = image;

    }

    @NonNull
    @Override
    public CharityListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.data_row, parent, false);
        return new CharityListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final CharityListViewHolder holder,final int position) {


        holder.titleText.setText(charityData[position]);
        holder.descText.setText(descriptionData[position]);
        holder.charityImage.setImageResource(images[position]);

        holder.charityLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.charityLayout.isSelected()) {
                    holder.charityLayout.setSelected(false);
                    System.out.println("Set to false");

                } else if(!holder.charityLayout.isSelected()){
                    holder.charityLayout.setSelected(true);
                    System.out.println("Set to true");
                }

                if(holder.charityLayout.isSelected()) {
                    holder.whiteBox.setBackgroundResource(R.drawable.bluebox);

                    DonateSelection.enableNextButton();
                    System.out.println("Blue Box");
                }

                if(!holder.charityLayout.isSelected()) {
                    holder.whiteBox.setBackgroundResource(R.drawable.box);

                    System.out.println("White Box");
                }
            }
        });

    }

    @Override
    public int getItemCount() {                                                        
        return images.length;
    }

    public class CharityListViewHolder extends RecyclerView.ViewHolder {
        TextView titleText, descText;
        ImageView charityImage;

        RelativeLayout whiteBox;
        RelativeLayout charityLayout;

        public CharityListViewHolder(@NonNull View itemView) {
            super(itemView);

            titleText = itemView.findViewById(R.id.titleText);
            descText = itemView.findViewById(R.id.descText);
            charityImage = itemView.findViewById(R.id.charityImage);
            whiteBox = itemView.findViewById(R.id.whiteBox);
            charityLayout = itemView.findViewById(R.id.charityLayout);


        }

    }
}

推荐答案

这是因为RecyclerView的工作原理.从屏幕上消失后,它将回收您的视图.您应该使用if/else来维持视图的正确状态.用下面的代码替换您的onClick操作.

This is because of how RecyclerView works. It recycles your view after it disappears from your screen. You should use if/else to maintain the right state of a view. Replace your onClick action with below code.

 holder.charityLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(holder.charityLayout.isSelected()) {
                        holder.charityLayout.setSelected(false);
                        System.out.println("Set to false");

                    } else {
                        holder.charityLayout.setSelected(true);
                        System.out.println("Set to true");
                    }

                    if(holder.charityLayout.isSelected()) {
                        holder.whiteBox.setBackgroundResource(R.drawable.bluebox);

                        DonateSelection.enableNextButton();
                        System.out.println("Blue Box");
                    }else{

                        holder.whiteBox.setBackgroundResource(R.drawable.box);

                        System.out.println("White Box");
                    }



                }
            });

已编辑这是原始代码.但是,我认为应该可以.您应该添加一个组成回收站的检查状态数组.默认情况下,它们全都是假的.然后,在onBindViewHolder()上检查它.单击时,将boolean设置为true并调用notifyDatasetChanged.

Edited Here is raw code.But, I think that should work. You should add an array of check state that recycler is composed of. By default they all will be false. Then, check it onBindViewHolder(). When click, set boolean to true and call notifyDatasetChanged.

public class CharityListAdapter extends RecyclerView.Adapter<CharityListAdapter.CharityListViewHolder> {
    String charityData[], descriptionData[];
    int images[]; boolean checkState[];
    Context context;

    public CharityListAdapter(Context ct, String charity[], String description[], int image[],boolean checkState[]) {
        context = ct;
        charityData = charity;
        descriptionData= description;
        images = image;
    checkState=checkState;  
    }

    @NonNull
    @Override
    public CharityListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.data_row, parent, false);
        return new CharityListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final CharityListViewHolder holder,final int position) {


        holder.titleText.setText(charityData[position]);
        holder.descText.setText(descriptionData[position]);
        holder.charityImage.setImageResource(images[position]);

    if(checkState[position]){
      holder.whiteBox.setBackgroundResource(R.drawable.bluebox);
          DonateSelection.enableNextButton();
    }else{
        holder.whiteBox.setBackgroundResource(R.drawable.box);
    }

        holder.charityLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        if(checkState[position]){
            checkState[position]=false;
        }else{
            checkState[position]=true;
        }
                 notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {                                                        
        return images.length;
    }

    public class CharityListViewHolder extends RecyclerView.ViewHolder {
        TextView titleText, descText;
        ImageView charityImage;

        RelativeLayout whiteBox;
        RelativeLayout charityLayout;

        public CharityListViewHolder(@NonNull View itemView) {
            super(itemView);

            titleText = itemView.findViewById(R.id.titleText);
            descText = itemView.findViewById(R.id.descText);
            charityImage = itemView.findViewById(R.id.charityImage);
            whiteBox = itemView.findViewById(R.id.whiteBox);
            charityLayout = itemView.findViewById(R.id.charityLayout);


        }

    }
}

这篇关于如何在RecyclerView中更改特定项目onClick的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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