CymChad / BaseRecyclerViewAdapterHelper

BRVAH:Powerful and flexible RecyclerAdapter
http://www.recyclerview.org/
MIT License
24.28k stars 5.15k forks source link

关于复用后导致错乱的问题 #3166

Open roybill opened 4 years ago

roybill commented 4 years ago

竖向RecyclerView列表中,采用的多布局,其中有一种布局是横向滑动的RecyclerView,当这个横向滑动的RecyclerView滑动到最后一个item的时候,点击item中的关注按钮,点击后变为"已关注",现在我外层这个RecyclerView往下滑,然后再滑动回来这个横向的RecyclerView,这时候横向的这个RecyclerView全部都变为"未关注"了.这个横向的RecyclerView我已经设置了点击的标记.下面是横向RecyclerView的点击和adapter代码: 1.adapter代码: `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);
    }

}

} 2.该RecyclerView的item点击: RecyclerView recyclerView = helper.getView(R.id.rv_home_focus_horizontal_list); List data = new ArrayList<>(); for (int i = 0; i < 5; i++) { FocusBean.DataBean.ListBean bean = new FocusBean.DataBean.ListBean(); data.add(bean); } LinearLayoutManager manager = new LinearLayoutManager(getContext()); manager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(manager); FocusHorizontalListAdapter adapter = new FocusHorizontalListAdapter(R.layout.multiple_item_focus_horizontal, data); 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(true);
                    adapter.notifyItemChanged(position);
                }
            });`
shuzhili commented 4 years ago

我也有这个问题你怎么解决的啊

roybill commented 4 years ago

@shuzhili 只需要把UI和数据绑定就行了