Adapter嵌套Adapter,里层的Adapter布局有一个checkBox,activity怎样把处理checkBox事件,写个祥细的例子
时间: 2024-04-03 22:32:58 浏览: 78
首先,我们需要在外层的Adapter中定义一个接口来处理内层Adapter中CheckBox的点击事件。在外层Adapter中添加以下代码:
```
public interface OnCheckedChangeListener {
void onCheckedChanged(boolean isChecked, int position);
}
```
然后,在外层Adapter的ViewHolder中设置内层Adapter的CheckBox监听器,并在CheckBox的回调中调用外层Adapter定义的接口。代码如下:
```
public class OuterAdapter extends RecyclerView.Adapter<OuterAdapter.ViewHolder> implements InnerAdapter.OnCheckedChangeListener {
private List<List<String>> mData;
private OnCheckedChangeListener mCheckedChangeListener;
public OuterAdapter(List<List<String>> data) {
mData = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_outer, parent, false);
ViewHolder holder = new ViewHolder(view);
holder.innerRecyclerView.setLayoutManager(new LinearLayoutManager(parent.getContext()));
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
InnerAdapter innerAdapter = new InnerAdapter(mData.get(position));
innerAdapter.setOnCheckedChangeListener(this);
holder.innerRecyclerView.setAdapter(innerAdapter);
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public void onCheckedChanged(boolean isChecked, int position) {
if (mCheckedChangeListener != null) {
mCheckedChangeListener.onCheckedChanged(isChecked, position);
}
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mCheckedChangeListener = listener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView innerRecyclerView;
ViewHolder(View itemView) {
super(itemView);
innerRecyclerView = itemView.findViewById(R.id.innerRecyclerView);
}
}
}
```
在内层Adapter中,我们需要定义一个接口,来处理CheckBox的点击事件。在CheckBox的回调中调用内层Adapter定义的接口。代码如下:
```
public class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> {
private List<String> mData;
private OnCheckedChangeListener mCheckedChangeListener;
public InnerAdapter(List<String> data) {
mData = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_inner, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.checkBox.setText(mData.get(position));
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mCheckedChangeListener != null) {
mCheckedChangeListener.onCheckedChanged(isChecked, position);
}
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mCheckedChangeListener = listener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
CheckBox checkBox;
ViewHolder(View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.checkBox);
}
}
public interface OnCheckedChangeListener {
void onCheckedChanged(boolean isChecked, int position);
}
}
```
最后,在Activity中设置外层Adapter的接口回调即可处理CheckBox的点击事件。代码如下:
```
public class MainActivity extends AppCompatActivity {
private List<List<String>> mData = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 3; i++) {
List<String> innerData = new ArrayList<>();
for (int j = 0; j < 5; j++) {
innerData.add("Item " + j);
}
mData.add(innerData);
}
OuterAdapter outerAdapter = new OuterAdapter(mData);
outerAdapter.setOnCheckedChangeListener(new OuterAdapter.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isChecked, int position) {
Log.d("MainActivity", "CheckBox at position " + position + " is " + (isChecked ? "checked" : "unchecked"));
}
});
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(outerAdapter);
}
}
```
这样,当CheckBox被点击时,会在控制台输出相应的信息。
阅读全文