項目中有需求,參考網(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());
? ? ? ? }
}
});