当前位置:   article > 正文

RecyclerView 里面实现RadioButton单选问题_android radiobutton recycleview

android radiobutton recycleview

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

子布局:

<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" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

适配器: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);
    
    • 1

    }

    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);
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    }

    @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);
}
  • 1
  • 2
  • 3
  • 4
  • 5

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/733759
推荐阅读
相关标签
  

闽ICP备14008679号