赞
踩
RecyclerView 里面实现RadioButton单选问题
主布局:
<RadioGroup
android:id="@+id/smallKindRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_kindsmallrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RadioGroup>
子布局:
<RadioButton
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="50dp"
android:layout_weight="1"
android:background="@drawable/radiobutton"
android:button="@null"
android:gravity="center"
android:text="猪肉类"
android:textSize="14sp" />
适配器:Adapter
package com.gxuwz.supplychain.adapter.kind;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
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 com.google.gson.Gson;
import com.gxuwz.supplychain.R;
import com.gxuwz.supplychain.activity.DishnameActivity;
import com.gxuwz.supplychain.activity.MarketActivity;
import com.gxuwz.supplychain.adapter.market.MarketViewAdapter;
import com.gxuwz.supplychain.adapter.market.MyRecyclerViewAdapter;
import com.gxuwz.supplychain.common.MySharedPreferences;
import com.gxuwz.supplychain.contants.Contants;
import com.gxuwz.supplychain.entity.business.TmKindSmall;
import com.gxuwz.supplychain.entity.business.TmVgtSell;
import com.gxuwz.supplychain.utility.ImgAsyncUtil;
import com.gxuwz.supplychain.utility.JUtils;
import com.gxuwz.supplychain.utility.ViewUtils;
import com.gxuwz.supplychain.utility.XutilHttpHelp;
import com.gxuwz.supplychain.utility.XutilRequestCallBack;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
Created by zhangxt4 on 2016/2/17.
*/
public class KindRecyclerViewAdapter extends RecyclerView.Adapter {
private Context mContext;
private LayoutInflater mInflater;
private List mDatas;
MarketActivity Mcontext;
ArrayList ischeck=new ArrayList<>();
private int index = -1;
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener listener) {
this.onItemClickListener = listener;
}
//构造方法
public KindRecyclerViewAdapter(Context context, List datas) {
this.mContext = context;
this.Mcontext= (MarketActivity) context;
this.mDatas = datas;
this.mInflater = LayoutInflater.from(context);
}
@Override
public KindViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_kindsmall_listview, parent, false);
KindViewHolder myViewHolder = new KindViewHolder(view);
return myViewHolder;
}
//在这里设置点击监听
@Override
public void onBindViewHolder(final KindViewHolder holder, final int position) {
TmKindSmall tmKindSmall = mDatas.get(position);
holder.kindSmall_iv.setText(tmKindSmall.getKindSmallName());
holder.kindSmallId.setText(tmKindSmall.getKindSmallId());
holder.kindSmall_iv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
index = position;
notifyDataSetChanged();
String kindSmallId= holder.kindSmallId.getText().toString();
Mcontext.smallKindRadioGroupCherk(kindSmallId);
}
}
});
if(index==position){
holder.kindSmall_iv.setChecked(true);
} else {
holder.kindSmall_iv.setChecked(false);
}
}
@Override
public int getItemCount() {
return mDatas.size();
}
}
class KindViewHolder extends RecyclerView.ViewHolder {
public RadioButton kindSmall_iv;
public TextView kindSmallId;
public KindViewHolder(View view) {
super(view);
kindSmall_iv = (RadioButton) view.findViewById(R.id.bt);
kindSmallId=(TextView)view.findViewById(R.id.kindSmallId);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。