垂直滑动的父RecyclerView中有多种布局,其中一种是水平滑动的RecyclerView,比如这个水平的RecyclerView在垂直父RecyclerView的第3条item和第7条item各有一个,我点击了第3条item中的水平RecyclerView的一个"关注",状态变为"已关注",这时候往下滑动到第7条item的水平RecyclerView,这时候对应的item也变成了"已关注",也就是说第3条和第7条水平的RecyclerView同步了,这种问题怎么解决呢?说起来比较抽象,下面是截图和代码:
1.截图:
2.水平RecyclerView的Adapter:
public class TempFiveAdapter extends BaseItemProvider<FocusBean.DataBean.ListBean> {
private FocusHorizontalListAdapter adapter;
@Override
public int getItemViewType() {
return FocusBean.DataBean.ListBean.FIFTH_TYPE;
}
@Override
public int getLayoutId() {
return R.layout.multiple_item_home_focus_horizontal_list;
}
@Override
public void convert(@NotNull BaseViewHolder helper, FocusBean.DataBean.ListBean listBean) {
RecyclerView recyclerView = helper.getView(R.id.rv_home_focus_horizontal_list);
List<FocusBean.DataBean.ListBean> data = new ArrayList<>();
for (int i = 0; i < 5; i++) {
FocusBean.DataBean.ListBean bean = new FocusBean.DataBean.ListBean();
data.add(bean);
}
if (adapter == null) {
LinearLayoutManager manager = new LinearLayoutManager(getContext());
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(manager);
adapter = new FocusHorizontalListAdapter(R.layout.multiple_item_focus_horizontal, data);
LogUtils.d("0855 55555");
recyclerView.setAdapter(adapter);
adapter.addChildClickViewIds(R.id.tv_group_focus);
adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
@Override
public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
Toast.makeText(getContext(), "点击了" + position, Toast.LENGTH_SHORT).show();
FocusBean.DataBean.ListBean listBean = data.get(position);
listBean.setFocused(listBean.isFocused() ? false : true);
adapter.notifyItemChanged(position);
}
});
}else{
// recyclerView.setAdapter(adapter);
}
}
}
3.水平RecyclerView子item的adpater:
public class FocusHorizontalListAdapter extends BaseQuickAdapter<FocusBean.DataBean.ListBean, BaseViewHolder> {
public FocusHorizontalListAdapter(int layoutResId, @Nullable List<FocusBean.DataBean.ListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(@NotNull BaseViewHolder helper, FocusBean.DataBean.ListBean listBean) {
TextView focusText = helper.getView(R.id.tv_group_focus);
boolean focused = listBean.isFocused();
if(focused){
focusText.setText("已关注");
focusText.setTextColor(Color.parseColor("#999999"));
focusText.setBackgroundResource(R.drawable.focused_shape);
}else{
focusText.setText("关注");
focusText.setTextColor(Color.parseColor("#FFFFFF"));
focusText.setBackgroundResource(R.drawable.unfocus_shape);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…