RecyclerView 中的单选 [英] Single selection in RecyclerView

查看:27
本文介绍了RecyclerView 中的单选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 RecyclerView 类中没有默认选择方法,但我尝试了以下方式:

I know there are no default selection methods in the RecyclerView class, but I have tried in the following way:

public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.mTextView.setText(fonts.get(position).getName());
    holder.checkBox.setChecked(fonts.get(position).isSelected());

    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked) {
                for (int i = 0; i < fonts.size(); i++) {
                    fonts.get(i).setSelected(false);
                }
                fonts.get(position).setSelected(isChecked);
            }
        }
    });
}

在尝试这段代码时,我得到了预期的输出,但并不完全.

While trying this code, I got the expected output, but not completely.

我会用图片来解释这一点.

I will explain this with images.

默认情况下,第一项是从我的适配器中选择的.

By default, the first item is selected from my adapter.

然后我选择第 2 个,然后是第 3 个,然后是第 4 个,最后是第 5 个.

Then I select the 2nd, then the 3rd, then the 4th and finally the 5th one.

这里只应选择第 5 个,但所有 5 个都被选中.

Here only the 5th should be selected, but all five are getting selected.

如果我将列表滚动到底部并再次回到顶部,我会得到我所期望的.

If I scroll the list to the bottom and come again to the top I get what I expect.

我怎样才能克服这个问题?有时,如果我非常快速地滚动列表,则会选择其他一些项目.如何克服这个问题?

How can I overcome this issue? And sometimes if I scroll the list very fast some other item gets selected. How to overcome this problem too?

当我尝试在 fonts.get(position).setSelected(isChecked); 之后使用 notifyDataSetChanged(); 我得到以下异常:

While I am trying to use notifyDataSetChanged() after fonts.get(position).setSelected(isChecked); I got following exception:

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
        at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:1462)
        at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:2982)
        at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:7493)
        at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:4338)
        at com.app.myapp.screens.RecycleAdapter.onRowSelect(RecycleAdapter.java:111)

推荐答案

问题的解决方法:

public class yourRecyclerViewAdapter extends RecyclerView.Adapter<yourRecyclerViewAdapter.yourViewHolder> {

    private static CheckBox lastChecked = null;
    private static int lastCheckedPos = 0;


    public void onBindViewHolder(ViewHolder holder, final int position) {
    
        holder.mTextView.setText(fonts.get(position).getName());
        holder.checkBox.setChecked(fonts.get(position).isSelected());
        holder.checkBox.setTag(new Integer(position));

        //for default check in first item
        if(position == 0 && fonts.get(0).isSelected() && holder.checkBox.isChecked())
        {
           lastChecked = holder.checkBox;
           lastCheckedPos = 0;
        }
           
        holder.checkBox.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
               CheckBox cb = (CheckBox)v;
               int clickedPos = ((Integer)cb.getTag()).intValue(); 

               if(cb.isChecked())
               {
                  if(lastChecked != null)
                  {
                      lastChecked.setChecked(false);
                      fonts.get(lastCheckedPos).setSelected(false);
                  }                       
                 
                  lastChecked = cb;
                  lastCheckedPos = clickedPos;
              }
              else
                 lastChecked = null;

              fonts.get(clickedPos).setSelected(cb.isChecked);
           }
       });
    }
}

这篇关于RecyclerView 中的单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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