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");
---------------------
作者:GuldanHu
來源:CSDN
原文:https://blog.csdn.net/GuldanHu/article/details/78689685
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM