ListView中加入RadioButton 当点击ListView的item时,选中里面的RadioButton

列表内容

ListView中加入RadioButton 当点击ListView的item时,选中里面的RadioButton


item的布局xml

注意:Android:descendantFocusability=”blocksDescendants”该属性的作用是直接覆盖子view的点击事件。不设置,子view就会获取到监听,item的监听将会失效

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"  android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#ffffff"
    android:descendantFocusability="blocksDescendants"
    >
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:gravity="center">
        <RadioButton

            android:id="@+id/item_group_member_checked"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"

            android:button="@null"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="3"
        android:orientation="vertical"
        android:gravity="center_vertical">

            <TextView
                android:textSize="20sp"
                android:id="@+id/item_group_member_join_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="加入时间:2016-01-21"
                android:textColor="@color/comment_text_color_s"/>
    </LinearLayout>

</LinearLayout>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

activity的布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ListView
            android:cacheColorHint="#00000000"
            android:id="@+id/proxy_group_member_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="false"
            android:dividerHeight="0.1dp"
            android:orientation="vertical"
            android:scrollbars="none">

        </ListView>

</LinearLayout>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

ListView的adapter java代码

注意:需要设置radioButton的setEnabled(false)否则 单点击radiobutton时,它的状态会保留下来



import android.media.Image;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.nbcbb.app.R;
import java.util.HashMap;


public class ListViewAdapter extends BaseAdapter{
    private List<String> data;
    private ViewHolder holder;
    private HashMap<String,Boolean> states=new HashMap<String,Boolean>();//记录所有radiobutton被点击的状态
    public ListViewAdapter(List<String> data){
    this.data=data;
}

    @Override
    public View getView(final int position,View convertView,ViewGroup parent){
        View view=convertView;
        if(view==null){
            convertView=getLayoutInflater().inflate(R.layout.item,parent,false);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        holder=new ViewHolder();

        holder.mChoosed=(RadioButton)convertView.findViewById(R.id.item_group_member_checked);
        holder.mJoinTime=(TextView)convertView.findViewById(R.id.item_group_member_join_time);



        holder.mJoinTime.setText(data.get(position).get("jointime"));

        boolean res=false;
        if(getStates(position)==null||
                getStates(position)==false)//判断当前位置的radiobutton点击状态
        {
            res=false;
            setStates(position, false);

        }else{
            res=true;
        }
        holder.mChoosed.setChecked(res);   
        return convertView;
    }
    //用于在activity中重置所有的radiobutton的状态
    public void clearStates(int position){
        // 重置,确保最多只有一项被选中
        for(String key:states.keySet()){
            states.put(key,false);
        }
        states.put(String.valueOf(position), true);
    }
    //用于获取状态值
    public Boolean getStates(int position){
        return states.get(String.valueOf(position));
    }
    //设置状态值
    public void setStates(int position,boolean isChecked){
        states.put(String.valueOf(position),false);
    }

    class ViewHolder{
        TextView mJoinTime;
        RadioButton mChoosed;
    }
}



 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

ListView的监听函数

mDetailListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                transfer(view, position);


            }
        });

private void transfer(View view,int position) {      
        RadioButton mRB=(RadioButton)view.findViewById(R.id.item_group_member_checked);
        //每次选择一个item时都要清除所有的状态,防止出现多个被选中
        mAdapter.clearStates(position);
        Log.i("CZ","position:"+position+",states:"+mAdapter.getStates(position));
        mRB.setChecked(mAdapter.getStates(position));
        //刷新数据,调用getView刷新ListView
        mAdapter.notifyDataSetChanged();

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值