滚动时,两个复选框处于recyclerview更改状态 [英] Two checkbox in recyclerview changing state when scrolling

查看:50
本文介绍了滚动时,两个复选框处于recyclerview更改状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑



通过删除两行来解决问题

  holder.checkbox.setChecked(mListenerList.get(position).isSelected()); 
holder.checkbox.setChecked(mListenerList.get(position).isSelected2());

并通过添加

  @Override 
public long getItemId(int position){
返回位置;
}

@Override
public int getItemViewType(int position){
返回位置;
}

我不为将来的开发人员编辑原始代码。



编辑之前的主要问题从这里开始



我正在研究幻想板球应用程序,我从11名球员的列表中选择队长和副队长。

我正在使用一个复选框来选择队长和副队长。



复选框的选择与我的代码配合正常,但是问题是当我选择第一名玩家作为队长(C),第二名玩家选择为副队长(VC),然后滚动列表时,复选框的状态正在改变



那么有什么正确的方法去做那件事吗?

我尝试了很多方法来解决这个问题单个复选框,但在我的情况下,只有两个复选框。



请参阅底部的屏幕截图以清楚查看。



适配器类



 公共类AdapterFinalTeamList扩展了RecyclerView.Adapter< AdapterFinalTeamList.MyViewHolder> {
private List< BeanDBTeam> mListenerList;
上下文mContext;
private CheckBox lastChecked = null;
private int lastCheckedPos = 0;

private CheckBox lastChecked2 = null;
private int lastCheckedPos2 = 0;

私人RadioButton lastCheckedRB = null;
private RadioButton lastCheckedRB1 = null;

TextView PreviousCaptain = null;
TextView PreviousVC = null;


public AdapterFinalTeamList(List< BeanDBTeam> mListenerList,Context context){
mContext = context;
this.mListenerList = mListenerList;

}

公共类MyViewHolder扩展了RecyclerView.ViewHolder {
TextView tv_PlayerName,tv_SelectCaptain,tv_SelectViceCaptain,tv_PlayerTeamName,tv_PlayerPoints,tv_TeamNumber;
ImageView im_PlayerImage,im_onetwox;
CheckBox checkbox,checkbox2;

RadioGroup无线电组;
RadioButton radio,radio2;


public MyViewHolder(View view){
super(view);

tv_PlayerName = view.findViewById(R.id.tv_PlayerName);
tv_PlayerTeamName = view.findViewById(R.id.tv_PlayerTeamName);
tv_PlayerPoints = view.findViewById(R.id.tv_PlayerPoints);

im_PlayerImage = view.findViewById(R.id.im_PlayerImage);
im_onetwox = view.findViewById(R.id.im_onetwox);


tv_TeamNumber = view.findViewById(R.id.tv_TeamNumber);
tv_SelectViceCaptain = view.findViewById(R.id.tv_SelectViceCaptain);
tv_SelectCaptain = view.findViewById(R.id.tv_SelectCaptain);
checkbox = view.findViewById(R.id.checkbox);
checkbox2 = view.findViewById(R.id.checkbox2);
radiogroup = view.findViewById(R.id.radiogroup);
radio = view.findViewById(R.id.radio);
radio2 = view.findViewById(R.id.radio2);

}

}

@Override
public int getItemCount(){
return mListenerList.size();
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_final_list,父级,false);

返回新的MyViewHolder(itemView);
}



@Override
public void onBindViewHolder(最终MyViewHolder持有人,最终int头寸){



字符串ID = mListenerList.get(position).getMatchId();

字符串arrayList =(mListenerList.get(position).getPlayerData());
try {
JSONObject job = new JSONObject(arrayList);

字符串PlayerName = job.getString( name);
String PlayerImage = job.getString( image);
字符串PlayerPoints = job.getString( player_points);
String PlayerCredit = job.getString( credit_points);
字符串TeamShortName = job.getString( team_short_name);

字符串team_number = job.getString( team_number);
字符串player_shortname = job.getString( player_shortname);
holder.tv_TeamNumber.setText(team_number);
// PlayerTeam = job.getString( short_name);

holder.tv_PlayerName.setText(PlayerName);

holder.tv_PlayerPoints.setText(PlayerPoints);
holder.tv_PlayerTeamName.setText(TeamShortName);


Glide.with(activity).load(Config.PLAYERIMAGE + PlayerImage)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.im_PlayerImage);

} catch(JSONException e){
e.printStackTrace();
}
holder.checkbox.setChecked(mListenerList.get(position).isSelected());
holder.checkbox.setTag(new Integer(position));

holder.checkbox.setChecked(mListenerList.get(position).isSelected2());
holder.checkbox2.setTag(new Integer(position));



holder.checkbox.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {

CheckBox cb =(CheckBox)v;
int clickedPos =((Integer)cb.getTag())。intValue();
holder.checkbox2.setChecked(false );

if(cb.isChecked()){
if(lastChecked!= null){
mListenerList.get(lastCheckedPos).setSelected(false);
lastChecked.setChecked(false);
}
否则,如果(clickedPos == position){
lastCheckedPos = clickedPos;
lastChecked = cb;
lastChecked.setChecked( true);
}
lastCheckedPos = clickedPos;
lastChecked = cb;
} else
lastChecked = null;

试试{
lastChecked.setChecked(true);
}
catch(异常e){
e.printStackTrace();
}

mListenerList.get(clickedPos).setSelected(cb.isChecked());
CaptainId = mListenerList.get(position).getPlayerId();


}
});
holder.checkbox2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
CheckBox cb =(CheckBox)v ;
int clickedPos =((Integer)cb.getTag())。intValue();

holder.checkbox.setChecked(false);

if( cb.isChecked()){
if(lastChecked2!= null){
lastChecked2.setChecked(false);
mListenerList.get(lastCheckedPos2).setSelected(false);
}
else if(clickedPos == position){
lastChecked2 = cb;
lastCheckedPos2 = clickedPos;
lastChecked2.setChecked(true);
}

lastChecked2 = cb;
lastCh eckedPos2 = clickedPos;
} else
lastChecked2 = null;

try {
lastChecked2.setChecked(true);
}
catch(异常e){
e.printStackTrace();
}
mListenerList.get(clickedPos).setSelected2(cb.isChecked());
ViceCaptainId = mListenerList.get(position).getPlayerId();

}

});


}

}

adapter_final_list.xml

 <?xml version = 1.0 encoding = utf-8 ?> 
< RelativeLayout
xmlns:android = http://schemas.android.com/apk/res / android
android:layout_width = match_parent
android:background = @ color / white
android:layout_height = wrap_content>

< RelativeLayout
android:layout_width = match_parent
android:layout_height = wrap_content
android:layout_margin = 2dp
android: padding = 5dp
android:id = @ + id / RL_PlayerListMain
android:elevation = 0dp>

< TextView
android:layout_width = wrap_content
android:layout_height = wrap_content
android:id = @ + id / tv_TeamNumber

android:visibility = invisible />
< ImageView
android:layout_width = 50dp
android:layout_height = 50dp
android:src = @ drawable / logo
android:layout_centerVertical = true
android:id = @ + id / im_PlayerImage />
< RelativeLayout
android:layout_width = wrap_content
android:layout_height = wrap_content
android:layout_centerVertical = true
android:id = @ + id / RL_Name
android:layout_toRightOf = @ + id / im_PlayerImage
android:layout_marginLeft = 10dp>
< TextView
android:layout_width = wrap_content
android:layout_height = wrap_content
android:text = Player Name
android:id = @ + id / tv_PlayerName
android:textColor =#1e1e1e
/>
< LinearLayout
android:layout_width = wrap_content
android:layout_height = wrap_content
android:orientation =水平
android:gravity = center
android:layout_below = @ + id / tv_PlayerName>
< TextView
android:layout_width = wrap_content
android:layout_height = wrap_content
android:text = IND
android:layout_gravity = center
android:layout_marginRight = 5dp
android:id = @ + id / tv_PlayerTeamName
android:textColor =#1e1e1e
/>
<查看
android:layout_width = 1dp
android:layout_height = 10dp
android:layout_gravity = center
android:background =# 8e8e8e />
< TextView
android:layout_width = wrap_content
android:layout_height = wrap_content
android:text = 55点
android:layout_gravity =中心
android:id = @ + id / tv_PlayerPoints
android:textColor =#8e8e8e
android:layout_marginLeft = 5dp
/>
< / LinearLayout>
< / RelativeLayout>
< RelativeLayout
android:layout_width = wrap_content
android:layout_height = wrap_content
android:layout_centerVertical = true
android:id = @ + id / RL_Credit
android:layout_alignParentRight = true>
< TextView
android:layout_width = 40dp
android:layout_height = 40dp
android:text = C
android:padding = 10dp
android:textAlignment = center
android:gravity = center
android:visibility = gone
android:layout_centerVertical = true
android :background = @ drawable / circle_captain_vc_back
android:id = @ + id / tv_SelectCaptain
android:textColor =#1e1e1e
android:layout_marginLeft = 10dp
/>

< CheckBox
android:layout_width = 40dp
android:layout_height = 40dp
android:layout_toRightOf = @ + id / tv_SelectViceCaptain
android:layout_centerVertical = true
android:visibility = visible
android:text = C
android:textColor =#1e1e1e
android:gravity = center
android:button = @ android:color / transparent
android:background = @ drawable / radio_selector
android:id = @ + id / checkbox / >
< CheckBox
android:layout_width = 40dp
android:layout_height = 40dp
android:layout_toRightOf = @ + id / checkbox
android: layout_centerVertical = true
android:visibility = visible
android:text = VC
android:layout_marginLeft = 5dp
android:textColor =#1e1e1e
android:gravity = center
android:button = @ android:color / transparent
android:background = @ drawable / radio_vc_selector
android:id = @ + id / checkbox2 />

< ImageView
android:layout_width = wrap_content
android:layout_height = wrap_content
android:src = @ drawable / onex_icon
android:visibility = invisible
android:layout_centerHorizo​​ntal = true
android:id = @ + id / im_onetwox
/>

< RadioGroup
android:layout_width = wrap_content
android:layout_height = wrap_content
android:id = @ + id / radiogroup
android:visibility = gone
android:layout_marginTop = 10dp
android:layout_below = @ + id / im_onetwox
android:orientation = horizo​​ntal>
< RadioButton
android:layout_width = 40dp
android:layout_height = 40dp
android:layout_toRightOf = @ + id / tv_SelectViceCaptain
android: layout_centerVertical = true
android:visibility = visible
android:text = C
android:layout_marginRight = 10dp
android:gravity = center
android:background = @ drawable / radio_selector
android:button = @ android:color / transparent
android:id = @ + id / radio />
< RadioButton
android:layout_width = 40dp
android:layout_height = 40dp
android:layout_toRightOf = @ + id / checkbox
android: layout_centerVertical = true
android:visibility = visible
android:text = VC
android:gravity = center
android:background = @ drawable / radio_vc_selector
android:button = @ android:color / transparent
android:id = @ + id / radio2 />
< / RadioGroup>



< TextView
android:layout_width = 40dp
android:layout_height = 40dp
android:text = VC
android:textAlignment = center
android:gravity = center
android:visibility = gone
android:padding = 10dp
android:layout_toRightOf = @ + id / tv_SelectCaptain
android:layout_centerVertical = true android:background = @ drawable / circle_captain_vc_back
android:id = @ + id / tv_SelectViceCaptain
android:textColor =#1e1e1e
android:layout_marginLeft = 10dp
/>


< / RelativeLayout>

< / RelativeLayout>

<查看
android:layout_width = match_parent
android:layout_height = 1dp
android:background =#8e8e8e
android :layout_marginTop = 5dp
android:layout_marginBottom = 2dp
android:layout_marginRight = 5dp
android:layout_marginLeft = 5dp
android:layout_below = @ + id / RL_PlayerListMain />
< / RelativeLayout>



选择C和VC





从上到下滚动后并自下而上





请忽略广播组。我的代码与复选框配合正常,只会在滚动时产生问题。

解决方案

对于 复选框 ,则需要使用 checkbox.OnCheckedChangeListener() 而不是 checkbox.setOnClickListener()



执行以下步骤



在您的 BeanDBTeam 类中添加一个新的布尔变量

  public class BeanDBTeam 
{
boolean isChecked;

public boolean getisChecked(){
return isChecked;
}

public void setIsChecked(boolean flag){
isChecked = flag;
}
}

现在在您的内心 onBindViewHolder() 添加以下代码

  @Override 
public void onBindViewHolder(@NonNull ViewHolder viewHolder,int position){
BeanDBTeam bean = mListenerList.get(position).getisChecked()

//在此处检查基于标志$的标志和复选框的状态b $ bholder.checkbox2.setChecked(bean.getisChecked());

holder.checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
//设置标志用户选中或取消选中复选框
bean.setIsChecked(isChecked);
notifyDataChanged();
}
}时列表中复选框状态的值;
}


Edit

Solved issue by removing two lines

holder.checkbox.setChecked(mListenerList.get(position).isSelected());
holder.checkbox.setChecked(mListenerList.get(position).isSelected2());

And By Adding

  @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

I am not editing my original code for future developers.

Main Question before Edit Start from Here

I am working on fantasy cricket app where I am selecting the captain and vice captain from a list of 11 player.
I am using a checkbox for selecting the captain and vice captain.

The selection of checkbox is working fine with my code, but the issue is when I select 1st player as a captain(C) and 2nd player as Vice-Captain(VC) and then scroll the list the checkbox state is changing and showing other player selected.

So is there any right way to do that thing?
I have tried many way they are working when there is single checkbox but in my case there is two and only one can be select from the list.

Please refer to the attached screenshots on the bottom for clarity.

Adapter Class

public class AdapterFinalTeamList extends     RecyclerView.Adapter<AdapterFinalTeamList.MyViewHolder> {
    private List<BeanDBTeam> mListenerList;
    Context mContext;
    private  CheckBox lastChecked = null;
    private  int lastCheckedPos = 0;

    private  CheckBox lastChecked2 = null;
    private  int lastCheckedPos2 = 0;

    private RadioButton lastCheckedRB = null;
    private RadioButton lastCheckedRB1 = null;

    TextView PreviousCaptain = null;
    TextView PreviousVC = null;


    public AdapterFinalTeamList(List<BeanDBTeam>              mListenerList, Context context) {
        mContext = context;
        this.mListenerList = mListenerList;

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_PlayerName,tv_SelectCaptain,tv_SelectViceCaptain, tv_PlayerTeamName, tv_PlayerPoints,tv_TeamNumber;
        ImageView im_PlayerImage,im_onetwox;
        CheckBox checkbox,checkbox2;

        RadioGroup radiogroup;
        RadioButton radio,radio2;


        public MyViewHolder(View view) {
            super(view);

            tv_PlayerName =view.findViewById(R.id.tv_PlayerName);
            tv_PlayerTeamName = view.findViewById(R.id.tv_PlayerTeamName);
            tv_PlayerPoints = view.findViewById(R.id.tv_PlayerPoints);

            im_PlayerImage = view.findViewById(R.id.im_PlayerImage);
            im_onetwox = view.findViewById(R.id.im_onetwox);


            tv_TeamNumber = view.findViewById(R.id.tv_TeamNumber);
            tv_SelectViceCaptain = view.findViewById(R.id.tv_SelectViceCaptain);
            tv_SelectCaptain= view.findViewById(R.id.tv_SelectCaptain);
            checkbox= view.findViewById(R.id.checkbox);
            checkbox2= view.findViewById(R.id.checkbox2);
            radiogroup= view.findViewById(R.id.radiogroup);
            radio= view.findViewById(R.id.radio);
            radio2= view.findViewById(R.id.radio2);

        }

    }

    @Override
    public int getItemCount() {
        return mListenerList.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_final_list, parent, false);

        return new MyViewHolder(itemView);
    }



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



        String id = mListenerList.get(position).getMatchId();

        String arrayList = (mListenerList.get(position).getPlayerData());
        try {
            JSONObject job = new JSONObject(arrayList);

            String PlayerName = job.getString("name");
            String PlayerImage = job.getString("image");
            String PlayerPoints = job.getString("player_points");
            String PlayerCredit = job.getString("credit_points");
            String TeamShortName = job.getString("team_short_name");

            String team_number = job.getString("team_number");
            String player_shortname = job.getString("player_shortname");
            holder.tv_TeamNumber.setText(team_number);
            // PlayerTeam= job.getString("short_name");

            holder.tv_PlayerName.setText(PlayerName);

            holder.tv_PlayerPoints.setText(PlayerPoints);
            holder.tv_PlayerTeamName.setText(TeamShortName);


            Glide.with(activity).load(Config.PLAYERIMAGE + PlayerImage)
                    .crossFade()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.im_PlayerImage);

        } catch (JSONException e) {
            e.printStackTrace();
        }
holder.checkbox.setChecked(mListenerList.get(position).isSelected());
        holder.checkbox.setTag(new Integer(position));

        holder.checkbox.setChecked(mListenerList.get(position).isSelected2());
        holder.checkbox2.setTag(new Integer(position));



        holder.checkbox.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {

                CheckBox cb = (CheckBox) v;
                int clickedPos = ((Integer) cb.getTag()).intValue();
                    holder.checkbox2.setChecked(false);

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

                    try {
                        lastChecked.setChecked(true);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }

                    mListenerList.get(clickedPos).setSelected(cb.isChecked());
                    CaptainId = mListenerList.get(position).getPlayerId();


            }
        });
    holder.checkbox2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                int clickedPos = ((Integer) cb.getTag()).intValue();

                holder.checkbox.setChecked(false);

                    if (cb.isChecked()) {
                        if (lastChecked2 != null) {
                            lastChecked2.setChecked(false);
                            mListenerList.get(lastCheckedPos2).setSelected(false);
                        }
                        else if (clickedPos==position){
                            lastChecked2 = cb;
                            lastCheckedPos2 = clickedPos;
                            lastChecked2.setChecked(true);
                        }

                        lastChecked2 = cb;
                        lastCheckedPos2 = clickedPos;
                    } else
                        lastChecked2 = null;

                    try{
                lastChecked2.setChecked(true);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    mListenerList.get(clickedPos).setSelected2(cb.isChecked());
                ViceCaptainId = mListenerList.get(position).getPlayerId();

                }

        });


    }

}

adapter_final_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res /android"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:padding="5dp"
    android:id="@+id/RL_PlayerListMain"
    android:elevation="0dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_TeamNumber"

    android:visibility="invisible"/>
<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/logo"
    android:layout_centerVertical="true"
    android:id="@+id/im_PlayerImage"/>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:id="@+id/RL_Name"
    android:layout_toRightOf="@+id/im_PlayerImage"
    android:layout_marginLeft="10dp">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Player Name"
    android:id="@+id/tv_PlayerName"
    android:textColor="#1e1e1e"
    />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_below="@+id/tv_PlayerName">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IND"
        android:layout_gravity="center"
        android:layout_marginRight="5dp"
        android:id="@+id/tv_PlayerTeamName"
        android:textColor="#1e1e1e"
        />
    <View
        android:layout_width="1dp"
        android:layout_height="10dp"
        android:layout_gravity="center"
        android:background="#8e8e8e"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="55 Points"
        android:layout_gravity="center"
        android:id="@+id/tv_PlayerPoints"
        android:textColor="#8e8e8e"
        android:layout_marginLeft="5dp"
        />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:id="@+id/RL_Credit"
    android:layout_alignParentRight="true">
    <TextView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="C"
        android:padding="10dp"
        android:textAlignment="center"
        android:gravity="center"
        android:visibility="gone"
        android:layout_centerVertical="true"
        android:background="@drawable/circle_captain_vc_back"
        android:id="@+id/tv_SelectCaptain"
        android:textColor="#1e1e1e"
        android:layout_marginLeft="10dp"
        />

    <CheckBox
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/tv_SelectViceCaptain"
        android:layout_centerVertical="true"
        android:visibility="visible"
        android:text="C"
        android:textColor="#1e1e1e"
        android:gravity="center"
        android:button="@android:color/transparent"
        android:background="@drawable/radio_selector"
        android:id="@+id/checkbox"/>
    <CheckBox
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/checkbox"
        android:layout_centerVertical="true"
        android:visibility="visible"
        android:text="VC"
        android:layout_marginLeft="5dp"
        android:textColor="#1e1e1e"
        android:gravity="center"
        android:button="@android:color/transparent"
        android:background="@drawable/radio_vc_selector"
        android:id="@+id/checkbox2"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/onex_icon"
        android:visibility="invisible"
        android:layout_centerHorizontal="true"
        android:id="@+id/im_onetwox"
        />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radiogroup"
        android:visibility="gone"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/im_onetwox"
        android:orientation="horizontal">
    <RadioButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/tv_SelectViceCaptain"
        android:layout_centerVertical="true"
        android:visibility="visible"
        android:text="C"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:background="@drawable/radio_selector"
        android:button="@android:color/transparent"
        android:id="@+id/radio"/>
    <RadioButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/checkbox"
        android:layout_centerVertical="true"
        android:visibility="visible"
        android:text="VC"
        android:gravity="center"
        android:background="@drawable/radio_vc_selector"
        android:button="@android:color/transparent"
        android:id="@+id/radio2"/>
    </RadioGroup>



    <TextView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="VC"
        android:textAlignment="center"
        android:gravity="center"
        android:visibility="gone"
        android:padding="10dp"
        android:layout_toRightOf="@+id/tv_SelectCaptain"
        android:layout_centerVertical="true"        android:background="@drawable/circle_captain_vc_back"
        android:id="@+id/tv_SelectViceCaptain"
        android:textColor="#1e1e1e"
        android:layout_marginLeft="10dp"
        />


</RelativeLayout>

</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#8e8e8e"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="2dp"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_below="@+id/RL_PlayerListMain"/>
 </RelativeLayout>

Selecting C and VC

After scrolling top to bottom and bottom to top

Please ignore Radio group. My code is working fine with checkbox, it's only creating issues when scrolling.

解决方案

For checkbox you need to use checkbox.OnCheckedChangeListener() instead of checkbox.setOnClickListener()

Follow this steps

add a new Boolean variable in your BeanDBTeam class

public class BeanDBTeam
{               
    boolean isChecked;    

    public boolean getisChecked() {
        return isChecked;
    }

    public void setIsChecked(boolean flag) {
        isChecked= flag;
    }
}

Now inside you onBindViewHolder() add below code

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
    BeanDBTeam  bean = mListenerList.get(position).getisChecked()

    // check here the flag and status of checkbox  based on flag
    holder.checkbox2.setChecked(bean.getisChecked());

    holder.checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
              // set flag of checkbox status  in your list when user check or uncheck the checkbox
             bean.setIsChecked(isChecked);
             notifyDataChanged();
          }
    }); 
 }            

这篇关于滚动时,两个复选框处于recyclerview更改状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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