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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年09月26日 21时25分37秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
1. 自动化运维系列之Cobbler自动装机
2019-06-07
ASP.NET MVC Model绑定(二)
2019-06-07
一步一步写算法(之hash表)
2019-06-07
漫谈并发编程(一) - 并发简单介绍
2019-06-07
JDBC连接MySQL数据库及演示样例
2019-06-07
System.currentTimeMillis();
2019-06-07
javascript中使用Map
2019-06-07
C# DataTable的詳細使用方法
2019-06-07
【转】Android的线程和线程池(AsyncTask)
2019-06-07
centos7 安装php7+mysql5.7+nginx+redis
2019-06-07
Ubuntu 14.04中文输入法的安装
2019-06-07
【分享】管理的最高境界是简单
2019-06-07
年关将至业内警示P2P跑路风险
2019-06-07
asp.net core刷新css缓存
2019-06-07
十大数据挖掘算法及各自优势
2019-06-07
python环境准备
2019-06-07
Invert (mirror) a bitmap
2019-06-07
LPC43xx SGPIO I2C Implementation
2019-06-07
Beta 冲刺(1/7)
2019-06-07
修改 Vultr 登录密码
2019-06-07