recycleview 做一個radiobutton的列表

項目中有需求,參考網(wǎng)上其他的文章寫了一篇,有很多不足之處,可以參考,如果有更好的想法,歡迎指點。好了開始吧

1.recycleview的布局

<android.support..v7.widget.RecyclerView

android:id="@+id/wuliu_recyclerview"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

2.數(shù)據(jù)

public class WuLiuBean{

public WuLiuBean(String name,boolean isSelected) { this.name = name; setSelected(isSelected); }

private String name;

private boolean isSelected;

public StringgetName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public boolean isSelected() {

return isSelected;

}

public void setSelected(boolean selected) {

isSelected = selected;

}

}

3.activity中添加數(shù)據(jù)

List datas =new ArrayList<>();

List?datasadd =new ArrayList<>();

for (int i =0; i <1; i++) {

datas.add(new WuLiuBean("快"));

? ? datas.add(new WuLiuBean("你呢", i ==0 ?true :false));

? ? datas.add(new WuLiuBean("方通"));

? ? datas.add(new WuLiuBean("各種痛"));

? ? datas.add(new WuLiuBean("天天"));

? ? datas.add(new WuLiuBean("麗麗"));

}

datasadd .addAll(datas?);

4.適配器布局

<LinearLayout

? xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

? ? android:layout_height="wrap_content" android:orientation="horizontal">

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:id="@+id/wuliu_rb_item"

? ? android:textDirection="rtl"

? ? android:gravity="center_vertical"

? ? android:padding="10dp"/>

5適配器

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.widget.RecyclerView;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.CompoundButton;

import android.widget.ImageView;

import android.widget.RadioButton;

import android.widget.TextView;

import java.util.List;

import cn.idcby.hwdemo.R;

import cn.idcby.hwdemo.activity.goods.SureGoodsActivity;

import cn.idcby.hwdemo.beans.WuLiuBean;

public class WuliuAdapterextends RecyclerView.Adapter {

private ListmDatas;

? ? private ContextmContext;

? ? private LayoutInflatermInflater;

? ? private int mSelectedPos = -1;//實現(xiàn)單選? 方法二,變量保存當(dāng)前選中的position

? ? private RecyclerViewmRv;//實現(xiàn)單選方法三: RecyclerView另一種定向刷新方法:

? ? public WuliuAdapter(List datas, Context context, RecyclerView rv) {

mDatas = datas;

? ? ? ? mContext = context;

? ? ? ? mInflater = LayoutInflater.from(mContext);

? ? ? ? mRv = rv;

? ? ? ? // 設(shè)置數(shù)據(jù)集時,找到默認(rèn)選中的pos

? ? ? ? for (int i =0; i

if (mDatas.get(i).isSelected()) {

mSelectedPos = i;

? ? ? ? ? ? }

}

}

@Override

? ? public CouponVHonCreateViewHolder(ViewGroup parent, int viewType) {

return new CouponVH(mInflater.inflate(R.layout.item_wuliu_adapter, parent, false));

? ? }

@Override

? ? public void onBindViewHolder(final CouponVH holder, final int position) {

? ? ? ? holder.mRB.setChecked(mDatas.get(position).isSelected());

? ? ? ? holder.mRB.setText(mDatas.get(position).getName());

? ? ? ? holder.mRB.setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View view) {

? ? ? ? ? ? ? ? CouponVH couponVH = (CouponVH)mRv.findViewHolderForLayoutPosition(mSelectedPos);

? ? ? ? ? ? ? ? if (couponVH !=null) {

? ? ? ? ? ? ? ? ? ? couponVH.mRB.setChecked(false);

? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? notifyItemChanged(mSelectedPos);

? ? ? ? ? ? ? ? }

mDatas.get(mSelectedPos).setSelected(false);

? ? ? ? ? ? ? ? mSelectedPos =position;

? ? ? ? ? ? ? ? mDatas.get(mSelectedPos).setSelected(true);

? ? ? ? ? ? ? ? holder.mRB.setChecked(true);


? ? ? ? ? ? ? ? if (subClickListener !=null) {

subClickListener.OntopicClickListener(mDatas.get(position), position);

? ? ? ? ? ? ? ? }


? ? ? ? ? ? }

});

? ? ? ? if (mDatas.size()>0){

if (holder.mRB.isChecked()){

if (subClickListener !=null) {

subClickListener.OntopicClickListener(mDatas.get(position), position);

? ? ? ? ? ? ? ? }

}

}

}

@Override

? ? public void onBindViewHolder(final CouponVH holder, int position, List payloads) {

? ? ? ? if (payloads.isEmpty()) {

onBindViewHolder(holder, position);

? ? ? ? }else {

Bundle payload = (Bundle) payloads.get(0);

? ? ? ? ? ? if (payload.containsKey("KEY_BOOLEAN")) {

final boolean aBoolean = payload.getBoolean("KEY_BOOLEAN");

? ? ? ? ? ? ? ? holder.mRB.setChecked(aBoolean);

? ? ? ? ? ? }

}

}

@Override

? ? public int getItemCount() {

return null !=mDatas ?mDatas.size() :0;

? ? }

public static class CouponVHextends RecyclerView.ViewHolder {

private RadioButtonmRB;

? ? ? ? public CouponVH(View itemView) {

super(itemView);

? ? ? ? ? ? mRB=(RadioButton)itemView.findViewById(R.id.wuliu_rb_item);

? ? ? ? }

}

////////////////////////////

? ? private SubClickListenersubClickListener;

? ? public void setsubClickListener(SubClickListener topicClickListener) {

this.subClickListener = topicClickListener;

? ? }

public interface SubClickListener {

void OntopicClickListener( WuLiuBean detail, int position);

? ? }

}

6activity中

mRecyclerview.setLayoutManager(new LinearLayoutManager(this));

WuliuAdapter wuliuAdapter=new WuliuAdapter(datasadd?, this, mRecyclerview);

mRecyclerview.setAdapter(wuliuAdapter);

wuliuAdapter.setsubClickListener(new WuliuAdapter.SubClickListener() {

@Override

? ? public void OntopicClickListener( WuLiuBean detail, int position) {

if (detail!=null){

//拿到返回的數(shù)據(jù)做進一步的處理

ComToastUtils.showToast(mContext,detail.getName());

? ? ? ? }

}

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • RecycleView的使用 RecyclerView是Google在API 21下support.V7包里的控件...
    安卓小生閱讀 10,980評論 1 20
  • Tangram是阿里出品、用于快速實現(xiàn)組合布局的框架模型,在手機天貓Android&iOS版 內(nèi)廣泛使用 該框架提...
    wintersweett閱讀 3,560評論 0 1
  • 這個學(xué)期起初的時候?qū)σ恍〇|西有所了解過,就跳過很多東西吧,把一些自己認(rèn)為重要的做出筆記,看了昨天的那東西,截圖搞得...
    六六的建斌閱讀 1,220評論 0 1
  • 你知道剩男剩女是如何剩下的嗎? 許多年后,他們都一樣,都會發(fā)現(xiàn)自己其實可能有過一次以上的機會脫單??僧?dāng)初他們?yōu)槭裁?..
    新小派自由行走的花閱讀 269評論 0 0
  • 真正意義上和馬森熟悉,是在葉落滿地的梧桐樹抽出新芽的時節(jié)。還記得那是大一剛開始的春日,排球賽的宣傳海報貼滿了校園宣...
    seven王王閱讀 527評論 0 1

友情鏈接更多精彩內(nèi)容