recycleview嵌套recycleview,并获取item点击的状态和数据

遇到一个需求是点击一个text框弹出一个选择的界面,上下可以滑动,左右也可以滑动,如下图

当时想的是写个recycleview,里面item根据数据的多少动态的添加,但是我想的是每一个item都要点击,而且判断选中状态,就用了recycleview嵌套recycleview,这个我是activity写的,上代码了。 ***activity的xml*** <LinearLayout android:background="#ffffff" android:layout_alignParentBottom="true" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:gravity="center" android:text="服务品类" android:textSize="22sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:textSize="20sp" android:layout_alignParentRight="true" android:id="@+id/quxiao" android:text="取消" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <android.support.v7.widget.RecyclerView android:layout_marginBottom="10dp" android:id="@+id/rv_real" android:layout_width="match_parent" android:layout_height="200dp"></android.support.v7.widget.RecyclerView> <Button android:layout_marginBottom="10dp" android:textColor="#fff" android:textSize="20sp" android:background="@drawable/bg_bb" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:layout_gravity="center" android:text="确定" android:id="@+id/bt_real" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>联网获取数据后拿第一层的数据设置适配器就可以了<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:textColor="#000" android:id="@+id/leibie" android:padding="10dp" android:textSize="18sp" android:background="#f2f2f2" android:text="类别" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:background="#999999" android:layout_width="match_parent" android:layout_height="1dp"/> <android.support.v7.widget.RecyclerView android:id="@+id/item_rv" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView></LinearLayout>在被嵌套的recycleview的adapter里定义接口 public void setOnServiceSelectInterface(OnServiceSelectInterface l){ mSelectListener = l; } public interface OnServiceSelectInterface{ void onServiceSelect(SkillBeanInfo.DataBean.SkillBean skillInfo); }}因为点击item的时候需要改变状态,所以就在数据bean里自己添加了状态的字段 /**是否选中*/ private boolean isSelect; public boolean isSelect() { return isSelect; } public void setSelect(boolean select) { isSelect = select; }item的点击事件 @Override public void onBindViewHolder(final ViewHolder holder, final int position) { final SkillBeanInfo.DataBean.SkillBean tagInfo = mList.get(position); holder.mMskill.setText(tagInfo.getSkillname()); holder.mMskill.setSelected(tagInfo.isSelect()); holder.mMskill.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mSelectListener!= null){ mSelectListener.onServiceSelect(tagInfo); } holder.mMskill.setSelected(!tagInfo.isSelect()); // 改变状态不这样的话,向下滑动的话状态会被重置,状态变成没有选择的样式,还可以你没有选择的也是被选中的样式 tagInfo.setSelect(!tagInfo.isSelect()); notifyItemChanged(position); } }); }在第一层的recycleview的adapter里实现写的接口 ItemRvAdapter itemRvAdapter = new ItemRvAdapter(mContext, dataBean.getSkill(), new ItemRvAdapter.OnServiceSelectInterface() { @Override public void onServiceSelect(SkillBeanInfo.DataBean.SkillBean skillInfo) { if(mSkillInfoList.size()==0){ mSkillInfoList.add(skillInfo); }else{ if(!mSkillInfoList.contains(skillInfo)){ mSkillInfoList.add(skillInfo); }else{ mSkillInfoList.remove(skillInfo); } } } }); holder.mSkill.setAdapter(itemRvAdapter); }在activity中将选择的数据传递出去 ArrayList<SkillBeanInfo.DataBean.SkillBean> skillBeanList = mPinleiAdapter.getmSkillInfoList(); Bundle bundle = new Bundle(); Intent intent = new Intent(); bundle.putSerializable("SERVLECT_SELECT_LIST",skillBeanList); intent.putExtras(bundle); setResult(RESULT_OK,intent); finish();获取数据的代码 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode!=RESULT_OK) return; if(requestCode==SERVICE_TYPE){ //TODO.. if(data!=null){ ArrayList<SkillBeanInfo.DataBean.SkillBean> mSelectList = (ArrayList<SkillBeanInfo.DataBean.SkillBean>) data.getSerializableExtra("SERVLECT_SELECT_LIST");

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值