//TabSwitchBean String title, boolean isChecked;
private ArrayList<TabSwitchBean> list = new ArrayList<>();
private void initTab() {
//将tablayout里面的清空 juejinTab--tablayout控件
juejinTab.removeAllTabs();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getisChecked()) {//如果集合中被选择
juejinTab.addTab(juejinTab.newTab().setText(list.get(i).getTitle()));//设置给TABLayout
}
}
}
适配器中
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
//先初始化状态
final TabSwitchBean bean = myBeans.get(position);
holder.aSwitch.setChecked(bean.getisChecked());
//给switch设置改变监听
holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
holder.aSwitch.setChecked(true);//改变控件状态
myBeans.get(position).setChecked(true);//改变集合数据
}else {
holder.aSwitch.setChecked(false);
myBeans.get(position).setChecked(false);
}
}
});
}