RecyclerView first and last item are changed, when clicking only one

走远了吗. 提交于 2021-02-04 19:16:47

问题


I'm using a recyclerview to display a list of interests one could choose from. Clicking the very first item makes the very last item also selected

Selecting first item:

Last item is also selected:

The selection is done with this code:

@Override
public InterestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.interests_textview, parent, false);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView textView = (TextView) v;
            if (textView.getCompoundDrawables()[2] == null) {
                textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);
            } else {
                textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }
        }
    });
    return new InterestViewHolder(v);
}

Also the very first item is also selected, when clicking the very last item. Who knows what could cause this?


回答1:


You are inflating a layout into a TextView. Change it to View v and it should work:

View v = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.interests_textview, parent, false);

Also you need to use findViewByID() in order to get the correct TextView from your layout:

Instead of

TextView textView = (TextView) v;

Do this:

TextView textView = findViewById(<id of your textview in the layout>);

Casting the View to a TextView is not what you want to do.




回答2:


I have a Checkbox to select items and the following String Arrays

data : holds all my items checkedData: holds all checked items

To solve this try editing the onBindViewHolder(holder , position)

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

    String title =data[position];
    holder.textTitle.setText(title);

    //logic to solve first and last (multiple) selection issue
    CheckBox chk = holder.chk;
    String current  = data[position];
    chk.setChecked(false);
    for(String st : checkedData){
        if(st.equals(current)){
            chk.setChecked(true);
        }
    }

    holder.setItemClickListener(new ItemClickListener()
    {
        @Override
        public void onItemClick(View v, int pos)
        {
            //here analomay occured 19:36 Custom Reycler view CheckBoxes | anomaly is now cleared.
            CheckBox chk = (CheckBox) v;//.findViewById(R.id.chk);

            if(chk.isChecked())
            {
                   checkedData.add(data[pos]);
            }else if(!chk.isChecked())
            {
                checkedData.remove(data[pos]);
            }



        }
    });
}

Explanation

What it will do is every time the data gets binded to holder in onBindViewHolder you previously uncheck the checkbox and then check if the corresponding string of that holder is present in checkedData array if yes set checkBox to true.



来源:https://stackoverflow.com/questions/50203508/recyclerview-first-and-last-item-are-changed-when-clicking-only-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!