浮生偷得半日闲,等接口定义的过程中,重新复习下adapter+listview实现单选的方法
主界面
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.locallistviewradiobutton.MainActivity" > 10 11 <ListView 12 android:id="@+id/lv_radiobutton" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:divider="@null"> 16 17 </ListView> 18 19 </RelativeLayout>
activity_listview.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="fill_parent" 5 android:layout_height="match_parent" 6 android:gravity="center" 7 android:orientation="vertical" > 8 9 <LinearLayout 10 android:id="@+id/ll_rg" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:orientation="horizontal" 14 android:background="#C6C6C6"> 15 16 <RadioButton 17 android:id="@+id/rb_select" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_gravity="center" 21 android:focusable="false"/> 22 23 <TextView 24 android:id="@+id/tv_name" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_gravity="center" 28 android:text="name" 29 android:textSize="20sp"/> 30 31 <ImageView 32 android:id="@+id/iv_card" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:scaleType="centerInside" 36 android:layout_marginLeft="20dp" 37 android:layout_marginRight="20dp" 38 android:layout_marginTop="2dp" 39 android:layout_marginBottom="2dp" 40 android:layout_gravity="left" 41 android:adjustViewBounds="true" 42 android:src="@drawable/ic_launcher" /> 43 44 </LinearLayout> 45 46 <View 47 android:layout_width="fill_parent" 48 android:layout_height="10dp" 49 android:background="#ffffff" /> 50 51 </LinearLayout>
MainActivity.java
1 package com.example.locallistviewradiobutton; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.AdapterView; 7 import android.widget.ListView; 8 import android.widget.AdapterView.OnItemClickListener; 9 10 11 public class MainActivity extends Activity { 12 private ListView lv_radiobutton; 13 private RBlistAdapter adapter; 14 private String[] names = new String[] { "a","b","c","d","e" }; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 lv_radiobutton = (ListView) findViewById(R.id.lv_radiobutton); 22 adapter = new RBlistAdapter(this, names); 23 lv_radiobutton.setAdapter(adapter); 24 25 //点击一行任意位置都能保证radiobutton选中 26 lv_radiobutton.setOnItemClickListener(new myOnItemClickListener()); 27 28 } 29 30 private class myOnItemClickListener implements OnItemClickListener { 31 @Override 32 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 33 adapter.index = position;//获取位置并传给adapter中的index(public变量) 34 adapter.notifyDataSetChanged(); 35 } 36 } 37 public void onBackPressed() { 38 // TODO Auto-generated method stub 39 super.onBackPressed(); 40 } 41 42 }
RBlistAdapter.java
1 package com.example.locallistviewradiobutton; 2 3 import android.widget.BaseAdapter; 4 import android.annotation.SuppressLint; 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.CompoundButton; 10 import android.widget.CompoundButton.OnCheckedChangeListener; 11 import android.widget.RadioButton; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 15 public class RBlistAdapter extends BaseAdapter{ 16 private LayoutInflater inflater; 17 private String[] picspecies; 18 private viewHolder holder; 19 // 标记用户当前选择的那一个,默认为第一个:(0) 20 public int index = 0; 21 private Context c; 22 23 public RBlistAdapter(Context c, String[] picspecies) { 24 super(); 25 this.c = c; 26 this.picspecies = picspecies; 27 inflater = LayoutInflater.from(c); 28 } 29 30 @Override 31 public int getCount() { 32 return picspecies.length; 33 } 34 35 @Override 36 public Object getItem(int position) { 37 return null; 38 } 39 40 @Override 41 public long getItemId(int position) { 42 return 0; 43 } 44 45 @SuppressLint("InflateParams") @Override 46 public View getView(final int position, View convertView, ViewGroup parent) { 47 48 holder = new viewHolder(); 49 if (convertView == null) { 50 convertView = inflater.inflate(R.layout.activity_listview, null); 51 holder.nameTxt = (TextView) convertView.findViewById(R.id.tv_name); 52 holder.selectBtn = (RadioButton) convertView.findViewById(R.id.rb_select); 53 convertView.setTag(holder); 54 } else { 55 holder = (viewHolder) convertView.getTag(); 56 } 57 58 holder.nameTxt.setText(picspecies[position]); 59 holder.selectBtn 60 .setOnCheckedChangeListener(new OnCheckedChangeListener() { 61 62 @Override 63 public void onCheckedChanged(CompoundButton buttonView, 64 boolean isChecked) { 65 if (isChecked) { 66 Toast.makeText(c, "您选择的是:" + picspecies[position], 67 Toast.LENGTH_SHORT).show(); 68 index = position; 69 notifyDataSetChanged(); 70 } 71 } 72 }); 73 74 if (index == position) {// 选中的条目和当前的条目是否相等 75 holder.selectBtn.setChecked(true); 76 } else { 77 holder.selectBtn.setChecked(false); 78 } 79 return convertView; 80 } 81 82 public class viewHolder { 83 public TextView nameTxt; 84 public RadioButton selectBtn; 85 } 86 87 }
时间: 2024-11-15 00:24:44