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");

要实现RecycleView嵌套RecycleView点击事件,需要在Adapter中设置点击事件监听器,并将其传递给子RecycleView的Adapter。 首先,在父RecycleView的Adapter中,实现一个接口来处理子RecycleView点击事件。例如: ``` public interface OnChildClickListener { void onChildClick(View view, int parentPosition, int childPosition); } ``` 然后,在父RecycleView的Adapter中,设置一个OnChildClickListener对象,并在点击事件中调用它: ``` public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.ViewHolder> { private ArrayList<ArrayList<String>> mData; private OnChildClickListener mChildClickListener; public ParentAdapter(ArrayList<ArrayList<String>> data) { mData = data; } public void setOnChildClickListener(OnChildClickListener listener) { mChildClickListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.parent_item, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { // set the data for each item in the view holder ArrayList<String> childData = mData.get(position); ChildAdapter childAdapter = new ChildAdapter(childData); childAdapter.setOnChildClickListener(new ChildAdapter.OnChildClickListener() { @Override public void onChildClick(View view, int childPosition) { // pass the click event to the parent adapter if (mChildClickListener != null) { mChildClickListener.onChildClick(view, position, childPosition); } } }); holder.childRecyclerView.setAdapter(childAdapter); } public static class ViewHolder extends RecyclerView.ViewHolder { RecyclerView childRecyclerView; public ViewHolder(View v) { super(v); childRecyclerView = (RecyclerView) v.findViewById(R.id.child_recycler_view); } } } ``` 在上面的代码中,我们在onBindViewHolder()方法中创建一个ChildAdapter,并将其设置为子RecycleView的Adapter。同时,我们还设置了一个OnChildClickListener对象,并在ChildAdapter中调用它来处理子RecycleView点击事件。 接下来,我们需要在ChildAdapter中设置一个OnChildClickListener对象,并在点击事件中调用它: ``` public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ViewHolder> { private ArrayList<String> mData; private OnChildClickListener mChildClickListener; public ChildAdapter(ArrayList<String> data) { mData = data; } public void setOnChildClickListener(OnChildClickListener listener) { mChildClickListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.child_item, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { // set the data for each item in the view holder String item = mData.get(position); holder.textView.setText(item); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // pass the click event to the parent adapter if (mChildClickListener != null) { mChildClickListener.onChildClick(view, position); } } }); } public static class ViewHolder extends RecyclerView.ViewHolder { TextView textView; public ViewHolder(View v) { super(v); textView = (TextView) v.findViewById(R.id.text_view); } } public interface OnChildClickListener { void onChildClick(View view, int childPosition); } } ``` 在上面的代码中,我们在onBindViewHolder()方法中设置了一个点击事件监听器,并在点击事件中调用了OnChildClickListener对象来处理点击事件。 最后,在Activity或Fragment中,我们需要设置ParentAdapter的OnChildClickListener对象,并在回调方法中处理点击事件: ``` parentAdapter.setOnChildClickListener(new ParentAdapter.OnChildClickListener() { @Override public void onChildClick(View view, int parentPosition, int childPosition) { // handle the child item click event here } }); ``` 在上面的代码中,我们设置了一个ParentAdapter的OnChildClickListener对象,并在回调方法中处理子RecycleView点击事件。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值