单选择在Re​​cyclerView [英] Single selection in RecyclerView

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

问题描述

我知道有在recyclerview类没有默认的选择方法,但我曾在下列方式,

I know there is no default selection methods in recyclerview class, But I have tried in 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);
            }
        }
    });
}

尽管试试这个code,我得到了预期的输出,但是完全没有。

While try this code, I got expected output, But completely not.

我将解释你的图像,

默认第一项是从我的适配器选择

By default first item is selected from my adapter

那么我想选择第二,然后第三个,然后第4和第5次终于之一,

then I am trying to select 2nd and then 3rd, and then 4th and finally 5th one,

这里第五只应选择,但所有五个

here 5th only should be selected, but all five.

如果我滚动列表底部,再来顶,

If I scroll the list to bottom and come again to top,

我得到了我所料,

我怎样才能解决这个问题。还有一些时间,如果我滚动速度非常快一些其他的项目被选中的列表中,如何解决这个问题了。

How can I overcome this issue. And some time If I scroll the list very fast some other item gets selected, How to overcome this problem too.

更新

当我试图使用 notifyDataSetChanged() fonts.get(位置).setSelected(器isChecked); 我得到了以下异常,

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)

任何解决方案将高度AP preciated。

Any solution will be highly appreciated.

感谢。

推荐答案

有关问题的解决方案:

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

希望这有助于!

Hope this help!

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

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