解決RecyclerView 中checkbox錯亂

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...
public class MainActivity extends AppCompatActivity {
private SparseBooleanArray mCheckStates = new SparseBooleanArray();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = C.f(this, R.id.rv);
LinearLayoutManager ll = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(ll);
rv.setAdapter(new RecyclerView.Adapter() {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = View.inflate(MainActivity.this, R.layout.rv_item_01, null);
LogUtils.e("onCreateViewHolder");
return new MyHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final MyHolder MyHolder = (MainActivity.MyHolder) holder;
MyHolder.tv.setText(DataFactory.getStrings().get(position));
MyHolder.cb.setTag(position);
MyHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = (int) buttonView.getTag();
if (isChecked) {
mCheckStates.put(pos, true);
} else {
mCheckStates.delete(pos);
}
}
});
MyHolder.cb.setChecked(mCheckStates.get(position, false));
LogUtils.e("onBindViewHolder"   position);
}
@Override
public int getItemCount() {
LogUtils.e("getItemCount");
return DataFactory.getStrings().size();
}
@Override
public int getItemViewType(int position) {
LogUtils.e("getItemViewType");
return super.getItemViewType(position);
}
});
}
class MyHolder extends RecyclerView.ViewHolder {
TextView tv;
CheckBox cb;
public MyHolder(View itemView) {
super(itemView);
tv = C.f(itemView, R.id.item_tv_1);
cb = C.f(itemView, R.id.item_cb_1);
LogUtils.e("MyHolder");
}
}
}

private SparseBooleanArray mCheckStates = new SparseBooleanArray();

MyHolder.cb.setTag(position);

int pos = (int) buttonView.getTag();

if (isChecked) { mCheckStates.put(pos, true); } else { mCheckStates.delete(pos); }

MyHolder.cb.setChecked(mCheckStates.get(position, false));

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/rv"
/>
</LinearLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/item_cb_1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:padding="10dp"
android:id="@ id/item_tv_1"/>
</LinearLayout>

 

 

IOS開發 最新文章