列表内容
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>();
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)
{
res=false;
setStates(position, false);
}else{
res=true;
}
holder.mChoosed.setChecked(res);
return convertView;
}
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);
mAdapter.clearStates(position);
Log.i("CZ","position:"+position+",states:"+mAdapter.getStates(position));
mRB.setChecked(mAdapter.getStates(position));
mAdapter.notifyDataSetChanged();
}