Android中Recyclew嵌套recycleview
发布日期:2021-05-27 02:48:57 浏览次数:20 分类:技术文章

本文共 5183 字,大约阅读时间需要 17 分钟。

思路:第一个recycleview的adapter里将所有用到的数据传进去,在第二个里面的convert方法里设置第二个adapter的数据,第二个adapter的recyclew写在第一个recycleview的item中,设置一个boolean值,convert方法中根据点击第一个recycleview的时候设为true显示第二个可见,再次点击设置false为不可见,通过给外部的点击回调控制boolean值,如在adapter这样写:

public void setOpenList(int position, boolean flag) {        this.openPosition = position;        isOpen = flag;        notifyDataSetChanged();    }

具体代码:

1.Adapter:

public class CoachEndAdapter extends BaseQuickAdapter
{ private Context mContext; private OnItemChildItemClickListener listener; private List
data; private int openPosition = -1; private boolean isOpen = false; public CoachEndAdapter(Context mContext, @Nullable List
data) { super(R.layout.item_coach_end, data); this.mContext = mContext; this.data = data; } @Override protected void convert(BaseViewHolder helper, CoachEndItem item) { helper.setVisible(R.id.tv_coach_end, false); int position = helper.getAdapterPosition(); helper.setText(R.id.rv_coach_end_city, item.getCity() + " (" + item.getStations().size() + ")"); if (position == openPosition) { //点击同一个会发送一个false过来 if (!isOpen) { helper.setVisible(R.id.rv_coach_end_station_list, false) .setBackgroundColor(R.id.rv_coach_end_city, mContext.getResources().getColor(R.color.white)); return; } helper.setVisible(R.id.rv_coach_end_station_list, true) .setBackgroundColor(R.id.rv_coach_end_city, mContext.getResources().getColor(R.color.grey_200)); RecyclerView stationList = helper.getView(R.id.rv_coach_end_station_list); stationList.setNestedScrollingEnabled(false); stationList.setHasFixedSize(true); stationList.setLayoutManager(new LinearLayoutManager(mContext)); CoachEndStationAdapter mAdapter = new CoachEndStationAdapter(item.getStations()); stationList.setAdapter(mAdapter); mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() { @Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position1) { CoachEndItem.StationsBean stationsBean = data.get(position).getStations().get(position1); if (listener != null) { listener.onItemChildItemClick(stationsBean); } } }); } else { helper.setVisible(R.id.rv_coach_end_station_list, false) .setBackgroundColor(R.id.rv_coach_end_city, mContext.getResources().getColor(R.color.white)); } } public void setOpenList(int position, boolean flag) { this.openPosition = position; isOpen = flag; notifyDataSetChanged(); } public class CoachEndStationAdapter extends BaseQuickAdapter
{ public CoachEndStationAdapter(@Nullable List
data) { super(R.layout.item_coach_end_station, data); } @Override protected void convert(BaseViewHolder helper, CoachEndItem.StationsBean item) { helper.setText(R.id.tv_station, item.getName()) .addOnClickListener(R.id.tv_station); } } public void setOnItemChildItemClickListener(OnItemChildItemClickListener listener) { this.listener = listener; } public interface OnItemChildItemClickListener { //列表的子列表的点击事件 void onItemChildItemClick(CoachEndItem.StationsBean stationBean); }}

2.第一个adapter的布局:

3.第二个adapter的布局:

4.activity使用

public void initRecyclerView() {        endList.setNestedScrollingEnabled(false);        endList.setHasFixedSize(true);        endList.setLayoutManager(new LinearLayoutManager(this));        mAdapter = new CoachEndAdapter(this, datas);        endList.setAdapter(mAdapter);        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {            @Override            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {                //第二次点击同一个则关闭                if (currentOpenPosition == position && isOpen) {                    isOpen = false;                    mAdapter.setOpenList(currentOpenPosition, false);                } else {                    isOpen = true;                    mAdapter.setOpenList(position, true);                    mAdapter.notifyDataSetChanged();                }                currentOpenPosition = position;            }        });        mAdapter.setOnItemChildItemClickListener(new CoachEndAdapter.OnItemChildItemClickListener() {            @Override            public void onItemChildItemClick(CoachEndItem.StationsBean stationBean) {                Intent intent = new Intent(CoachEndActivity.this, CoachActivity.class);                intent.putExtra("end", stationBean);                setResult(RESULT_OK, intent);                finish();            }        });    }

在这里插入图片描述

转载地址:https://blog.csdn.net/kururunga/article/details/86570374 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:公司年会记个坑
下一篇:Android加固后app检查版本更新解析错误的问题解决

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年09月26日 21时25分37秒