android 自定义ListView 使其带单选框按钮,并解决item点击不响应的问题

来源:互联网 发布:手机淘宝修改收货地址 编辑:程序博客网 时间:2024/05/29 11:21
原文出处:http://blog.csdn.net/wolaizhaomengxiang/article/details/22719601
自己定义ListView就要自己写个LIstviewAdapter继承BaseAdapter这个类然后通过其中的getView()方法把数据映射到相应控件之中

下面是我实现的带单选框的listView(如果是想带按钮实现更简单,就不需要考虑单选框的按钮特性了)

在做的过程之中出现了点击item无响应的情况,最后发现是子控件radioButton获得了焦点是其父控件即item失去了焦点所以在getView ()之中让RadioButton失去焦点即可

先放个效果图

代码如下

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.mylistview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.app.AlertDialog;  
  10. import android.content.Context;  
  11. import android.content.DialogInterface;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.view.LayoutInflater;  
  15. import android.view.View;  
  16. import android.view.ViewGroup;  
  17. import android.widget.AdapterView;  
  18. import android.widget.AdapterView.OnItemClickListener;  
  19. import android.widget.BaseAdapter;  
  20. import android.widget.Button;  
  21. import android.widget.CompoundButton;  
  22. import android.widget.ImageView;  
  23. import android.widget.ListView;  
  24. import android.widget.RadioButton;  
  25. import android.widget.TextView;  
  26.   
  27. public class MyListViewWithRadio extends Activity implements OnItemClickListener {  
  28.   
  29.     private ListView listView;  
  30.     private List<Map<String, Object>> mData;  
  31.       
  32.     //record the current checked radio number  
  33.     private int checkedIndex = -1;  
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.           
  38.         mData = getData();  
  39.         listView = new ListView(this);        
  40.         MyAdapter myAdapter = new MyAdapter(this);  
  41.         listView.setAdapter(myAdapter);  
  42.         listView.setOnItemClickListener(this);  
  43.         setContentView(listView);  
  44.     }  
  45.       
  46.     //获取要在list中显示的数据  
  47.     private List<Map<String,Object>> getData(){  
  48.         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();  
  49.         Map<String,Object> map = new HashMap<String,Object>();  
  50.           
  51.         map.put("title""G1");  
  52.         map.put("info""google 1");  
  53.         map.put("img", R.drawable.wear_breast);  
  54.         list.add(map);  
  55.   
  56.         map = new HashMap<String, Object>();  
  57.         map.put("title""G2");  
  58.         map.put("info""google 2");  
  59.         map.put("img", R.drawable.wear_wasit);  
  60.         list.add(map);  
  61.   
  62.         map = new HashMap<String, Object>();  
  63.         map.put("title""G3");  
  64.         map.put("info""google 3");  
  65.         map.put("img", R.drawable.wear_wrist);  
  66.         list.add(map);  
  67.           
  68.         map = new HashMap<String, Object>();  
  69.         map.put("title""G4");  
  70.         map.put("info""google 4");  
  71.         map.put("img", R.drawable.wearl_ankle);  
  72.         list.add(map);  
  73.           
  74.         map = new HashMap<String, Object>();  
  75.         map.put("title""G5");  
  76.         map.put("info""google 5");  
  77.         map.put("img", R.drawable.wear_breast);  
  78.         list.add(map);  
  79.   
  80.         map = new HashMap<String, Object>();  
  81.         map.put("title""G6");  
  82.         map.put("info""google 6");  
  83.         map.put("img", R.drawable.wear_wasit);  
  84.         list.add(map);  
  85.   
  86.         map = new HashMap<String, Object>();  
  87.         map.put("title""G7");  
  88.         map.put("info""google 7");  
  89.         map.put("img", R.drawable.wear_wrist);  
  90.         list.add(map);  
  91.           
  92.         map = new HashMap<String, Object>();  
  93.         map.put("title""G8");  
  94.         map.put("info""google 8");  
  95.         map.put("img", R.drawable.wearl_ankle);  
  96.         list.add(map);  
  97.           
  98.         map = new HashMap<String, Object>();  
  99.         map.put("title""G9");  
  100.         map.put("info""google 9");  
  101.         map.put("img", R.drawable.wear_breast);  
  102.         list.add(map);  
  103.   
  104.         map = new HashMap<String, Object>();  
  105.         map.put("title""G10");  
  106.         map.put("info""google 10");  
  107.         map.put("img", R.drawable.wear_wasit);  
  108.         list.add(map);  
  109.   
  110.         map = new HashMap<String, Object>();  
  111.         map.put("title""G11");  
  112.         map.put("info""google 11");  
  113.         map.put("img", R.drawable.wear_wrist);  
  114.         list.add(map);  
  115.           
  116.         map = new HashMap<String, Object>();  
  117.         map.put("title""G12");  
  118.         map.put("info""google 12");  
  119.         map.put("img", R.drawable.wearl_ankle);  
  120.         list.add(map);  
  121.           
  122.         return list;  
  123.     }  
  124.       
  125.   
  126.     //实现点击整个item时  都会触发单选框的 动作  
  127.     @Override  
  128.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  129.         // arg0:当前的listview。arg1:当前点击的item的句柄,arg2:当前item在listview中的position  
  130.         //arg3:当前listview中行数一般和position相等  
  131.         System.out.println("取position和id的值:"+arg2+"--"+arg3);  
  132.         ListView lv = (ListView)arg0;  
  133.               if(checkedIndex != arg2){   
  134.               {  //定位到现在处于点击状态的radio  
  135.                   System.out.println("checkedIndex != arg2");  
  136.                   int childId = checkedIndex - lv.getFirstVisiblePosition();    
  137.                   if(childId >= 0){  //如果checked =true的radio在显示的窗口内,改变其状态为false  
  138.                       View item = lv.getChildAt(childId);    
  139.                       if(item != null){    
  140.                           RadioButton rb = (RadioButton)item.findViewById(checkedIndex);    
  141.                           if(rb != null)    
  142.                           rb.setChecked(false);    
  143.                       }    
  144.                   }  
  145.                   //将当前点击的radio的checked变为true  
  146.                   RadioButton rb1 = (RadioButton)arg1.findViewById(arg2);    
  147.                   if(rb1 != null)    
  148.                   rb1.setChecked(true);  
  149.                   checkedIndex = arg2;  
  150.               }  
  151.               }   
  152.     }  
  153.           
  154.     //存放listview中的数据项  
  155.     public final class ViewHolder{  
  156.         public ImageView img;  
  157.         public TextView title;  
  158.         public TextView info;  
  159.         public RadioButton viewBtn;  
  160.     }  
  161.       
  162.     //自定义listview adapter  
  163.     public class MyAdapter extends BaseAdapter{  
  164.   
  165.         private LayoutInflater mInflater;  
  166.           
  167.           
  168.         public MyAdapter(Context context){  
  169.             this.mInflater = LayoutInflater.from(context);  
  170.         }  
  171.         @Override  
  172.         public int getCount() {  
  173.             // TODO Auto-generated method stub  
  174.             return mData.size();  
  175.         }  
  176.   
  177.         @Override  
  178.         public Object getItem(int arg0) {  
  179.             // TODO Auto-generated method stub  
  180.             return null;  
  181.         }  
  182.   
  183.         @Override  
  184.         public long getItemId(int arg0) {  
  185.             // TODO Auto-generated method stub  
  186.             return 0;  
  187.         }  
  188.   
  189.         @Override  
  190.         public View getView(int position, View convertView, ViewGroup parent) {  
  191.               
  192.             ViewHolder holder = null;  
  193.             if (convertView == null) {//convertView 存放item的  
  194.                 holder=new ViewHolder();              
  195.                 convertView = mInflater.inflate(R.layout.activity_mylistviewwithradio, null);  
  196.                 holder.img = (ImageView)convertView.findViewById(R.id.img2);  
  197.                 holder.title = (TextView)convertView.findViewById(R.id.title2);  
  198.                 holder.info = (TextView)convertView.findViewById(R.id.info2);  
  199.                 holder.viewBtn = (RadioButton)convertView.findViewById(R.id.listview2_radiobutton);  
  200.                 convertView.setTag(holder);  
  201.                   
  202.             }else {  
  203.                   
  204.                 holder = (ViewHolder)convertView.getTag();  
  205.             }  
  206.               
  207.               
  208.             holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));  
  209.             holder.title.setText((String)mData.get(position).get("title"));  
  210.             holder.info.setText((String)mData.get(position).get("info"));  
  211.               
  212.             //让子控件button失去焦点  这样不会覆盖掉item的焦点  否则点击item  不会触发响应即onItemClick失效  
  213.             holder.viewBtn.setFocusable(false);//无此句点击item无响应的  
  214.             holder.viewBtn.setId(position);    
  215.             holder.viewBtn.setChecked(position == checkedIndex);    
  216.               
  217.             holder.viewBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {    
  218.                 @Override    
  219.                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    
  220.                     if(isChecked){    
  221.                         //set pre radio button    
  222.                         if(checkedIndex != -1)    
  223.                         {    
  224.                             int childId = checkedIndex - listView.getFirstVisiblePosition();    
  225.                             if(childId >= 0){    
  226.                                 View item = listView.getChildAt(childId);    
  227.                                 if(item != null){    
  228.                                     RadioButton rb = (RadioButton)item.findViewById(checkedIndex);    
  229.                                     if(rb != null)    
  230.                                     rb.setChecked(false);    
  231.                                 }    
  232.                             }      
  233.                         }    
  234.                         //set cur radio button    
  235.                         checkedIndex = buttonView.getId();    
  236.                     }    
  237.                 }    
  238.             });    
  239.             return convertView;  
  240.         }  
  241.           
  242.     }  
  243.   
  244.   
  245. }  
layout中的XML代码如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:orientation="horizontal"   
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent">  
  6.   
  7. <ImageView android:id="@+id/img2"   
  8. android:layout_width="80dp"  
  9. android:layout_height="wrap_content"   
  10. android:layout_margin="5px"/>  
  11.   
  12. <LinearLayout android:orientation="vertical"  
  13. android:layout_width="80dp"   
  14. android:layout_height="wrap_content">  
  15.   
  16. <TextView android:id="@+id/title2"   
  17. android:layout_width="wrap_content"  
  18. android:layout_height="wrap_content"   
  19. android:textSize="22px" />  
  20. <TextView android:id="@+id/info2"   
  21. android:layout_width="wrap_content"  
  22. android:layout_height="wrap_content"   
  23. android:textSize="13px" />  
  24.   
  25. </LinearLayout>  
  26.   
  27. <RadioButton  
  28.    android:id="@+id/listview2_radiobutton"  
  29.    android:layout_width="80dp"  
  30.    android:layout_height="wrap_content"  
  31.    android:layout_gravity="bottom|right" />  
  32.   
  33.   
  34. </LinearLayout>  
0 0
原创粉丝点击