• 【转】为ListView每个Item上面的按钮添加事件


    原文网址:http://blog.csdn.net/qq435757399/article/details/8256453

    1.先看下效果图:

          

    在这里仅供测试,我把数据都写死了,根据需要可以自己进行修改,此外实现ListView上面每个Item上的Button的事件有两种方法:

    1.使用final变量扩展局部变量的生命周期范围主要代码(本文最后附全部代码):

    [java] view plain copy
     
    1. //注意原本getView方法中的int position变量是非final的,现在改为final  
    2.         @Override  
    3.         public View getView(final int position, View convertView, ViewGroup parent) {  
    4.              ViewHolder holder = null;  
    5.             if (convertView == null) {  
    6.                   
    7.                 holder=new ViewHolder();    
    8.                   
    9.                 //可以理解为从vlist获取view  之后把view返回给ListView  
    10.                 convertView = mInflater.inflate(R.layout.vlist, null);  
    11.                 holder.title = (TextView)convertView.findViewById(R.id.title);  
    12.                 holder.info = (TextView)convertView.findViewById(R.id.info);  
    13.                 holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);  
    14.                 convertView.setTag(holder);               
    15.             }else {               
    16.                 holder = (ViewHolder)convertView.getTag();  
    17.             }         
    18.               
    19.             holder.title.setText((String)mData.get(position).get("title"));  
    20.             holder.info.setText((String)mData.get(position).get("info"));  
    21.             holder.viewBtn.setTag(position);  
    22.             //给Button添加单击事件  添加Button之后ListView将失去焦点  需要的直接把Button的焦点去掉  
    23.             holder.viewBtn.setOnClickListener(new View.OnClickListener() {  
    24.                   
    25.                 @Override  
    26.                 public void onClick(View v) {  
    27.                     showInfo(position);                   
    28.                 }  
    29.             });  
    30.               
    31.             //holder.viewBtn.setOnClickListener(MyListener(position));  
    32.                       
    33.             return convertView;  
    34.         }  
    35.     }  
    36.       
    37.     //提取出来方便点  
    38.     public final class ViewHolder {  
    39.         public TextView title;  
    40.         public TextView info;  
    41.         public Button viewBtn;  
    42.     }  
    43.     public void showInfo(int position){  
    44.           
    45.         ImageView img=new ImageView(ListViewActivity.this);  
    46.         img.setImageResource(R.drawable.b);  
    47.         new AlertDialog.Builder(this).setView(img)  
    48.         .setTitle("详情"+position)  
    49.         .setMessage("菜名:"+title[position]+"   价格:"+info[position])  
    50.         .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
    51.             @Override  
    52.             public void onClick(DialogInterface dialog, int which) {  
    53.             }  
    54.         })  
    55.         .show();  
    56.     }  
    57.       



    2.使用类记录每个Button的位置,使每个BUTTON都有自己的Listener主要代码:

    [java] view plain copy
     
    1. //****************************************第二种方法,高手一般都用此种方法,具体原因,我还不清楚,有待研究  
    2.       
    3.         public View getView(int position, View convertView, ViewGroup parent) {  
    4.              ViewHolder holder = null;  
    5.              MyListener myListener=null;  
    6.             if (convertView == null) {  
    7.                   
    8.                 holder=new ViewHolder();    
    9.                   
    10.                 //可以理解为从vlist获取view  之后把view返回给ListView  
    11.                  myListener=new MyListener(position);  
    12.                      
    13.                 convertView = mInflater.inflate(R.layout.vlist, null);  
    14.                 holder.title = (TextView)convertView.findViewById(R.id.title);  
    15.                 holder.info = (TextView)convertView.findViewById(R.id.info);  
    16.                 holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);  
    17.                 convertView.setTag(holder);               
    18.             }else {               
    19.                 holder = (ViewHolder)convertView.getTag();  
    20.             }         
    21.               
    22.             holder.title.setText((String)mData.get(position).get("title"));  
    23.             holder.info.setText((String)mData.get(position).get("info"));  
    24.             holder.viewBtn.setTag(position);  
    25.             //给Button添加单击事件  添加Button之后ListView将失去焦点  需要的直接把Button的焦点去掉  
    26.             holder.viewBtn.setOnClickListener( myListener);  
    27.               
    28.             //holder.viewBtn.setOnClickListener(MyListener(position));  
    29.                       
    30.             return convertView;  
    31.         }  
    32.     }  
    33.       
    34.      private class MyListener implements OnClickListener{  
    35.             int mPosition;  
    36.             public MyListener(int inPosition){  
    37.                 mPosition= inPosition;  
    38.             }  
    39.             @Override  
    40.             public void onClick(View v) {  
    41.                 // TODO Auto-generated method stub  
    42.                 Toast.makeText(ListViewActivity.this, title[mPosition], Toast.LENGTH_SHORT).show();  
    43.             }  
    44.               
    45.         }  
    46.   
    47.       
    48.       
    49.       
    50.       
    51.       
    52.       
    53.       
    54.       
    55.       
    56.     //提取出来方便点  
    57.     public final class ViewHolder {  
    58.         public TextView title;  
    59.         public TextView info;  
    60.         public Button viewBtn;  
    61.     }  



    3.全部代码

    1.ListViewActivity.java全部代码:

    [java] view plain copy
     
    1. package ms.ListView;  
    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.view.LayoutInflater;  
    14. import android.view.View;  
    15. import android.view.View.OnClickListener;  
    16. import android.view.ViewGroup;  
    17. import android.widget.AdapterView;  
    18. import android.widget.AdapterView.OnItemSelectedListener;  
    19. import android.widget.BaseAdapter;  
    20. import android.widget.Button;  
    21. import android.widget.ImageView;  
    22. import android.widget.ListView;  
    23. import android.widget.TextView;  
    24. import android.widget.Toast;  
    25.   
    26. public class ListViewActivity extends Activity {  
    27.     /** Called when the activity is first created. */  
    28.     private List<Map<String, Object>> mData;  
    29.     private int flag;  
    30.     public static String title[]=new String[]{"菜名0","菜名1","菜名2","菜名3","菜名4","菜名5","菜名6","菜名7","菜名8","菜名9"};  
    31.     public static String info[]=new String[]{ "¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28",};  
    32.       
    33.   
    34.     @Override  
    35.     public void onCreate(Bundle savedInstanceState) {  
    36.         super.onCreate(savedInstanceState);  
    37.         setContentView(R.layout.main);  
    38.         mData = getData();  
    39.         ListView listView = (ListView) findViewById(R.id.listView);  
    40.         MyAdapter adapter = new MyAdapter(this);  
    41.         listView.setAdapter(adapter);  
    42.           
    43.           
    44.     }  
    45.   
    46.   
    47.   
    48.     //获取动态数组数据  可以由其他地方传来(json等)  
    49.     private List<Map<String, Object>> getData() {  
    50.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
    51. for(int i=0;i<title.length;i++){  
    52.         Map<String, Object> map = new HashMap<String, Object>();  
    53.         map.put("title", title[i]);  
    54.         map.put("info", info[i]);  
    55.         list.add(map);  
    56. }  
    57.   
    58.         return list;  
    59.     }  
    60.   
    61.     public class MyAdapter extends BaseAdapter {  
    62.   
    63.         private LayoutInflater mInflater;  
    64.   
    65.         public MyAdapter(Context context) {  
    66.             this.mInflater = LayoutInflater.from(context);  
    67.         }  
    68.   
    69.         @Override  
    70.         public int getCount() {  
    71.             // TODO Auto-generated method stub  
    72.             return mData.size();  
    73.         }  
    74.   
    75.         @Override  
    76.         public Object getItem(int position) {  
    77.             // TODO Auto-generated method stub  
    78.             return null;  
    79.         }  
    80.   
    81.         @Override  
    82.         public long getItemId(int position) {  
    83.             // TODO Auto-generated method stub  
    84.             return 0;  
    85.         }  
    86.         //****************************************final方法  
    87. //注意原本getView方法中的int position变量是非final的,现在改为final  
    88.         @Override  
    89.         public View getView(final int position, View convertView, ViewGroup parent) {  
    90.              ViewHolder holder = null;  
    91.             if (convertView == null) {  
    92.                   
    93.                 holder=new ViewHolder();    
    94.                   
    95.                 //可以理解为从vlist获取view  之后把view返回给ListView  
    96.                   
    97.                 convertView = mInflater.inflate(R.layout.vlist, null);  
    98.                 holder.title = (TextView)convertView.findViewById(R.id.title);  
    99.                 holder.info = (TextView)convertView.findViewById(R.id.info);  
    100.                 holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);  
    101.                 convertView.setTag(holder);               
    102.             }else {               
    103.                 holder = (ViewHolder)convertView.getTag();  
    104.             }         
    105.               
    106.             holder.title.setText((String)mData.get(position).get("title"));  
    107.             holder.info.setText((String)mData.get(position).get("info"));  
    108.             holder.viewBtn.setTag(position);  
    109.             //给Button添加单击事件  添加Button之后ListView将失去焦点  需要的直接把Button的焦点去掉  
    110.             holder.viewBtn.setOnClickListener(new View.OnClickListener() {  
    111.                   
    112.                 @Override  
    113.                 public void onClick(View v) {  
    114.                     showInfo(position);                   
    115.                 }  
    116.             });  
    117.               
    118.             //holder.viewBtn.setOnClickListener(MyListener(position));  
    119.                       
    120.             return convertView;  
    121.         }  
    122.     }  
    123.         //****************************************第二种方法,高手一般都用此种方法,具体原因,我还不清楚,有待研究  
    124.       
    125. //      public View getView(int position, View convertView, ViewGroup parent) {  
    126. //           ViewHolder holder = null;  
    127. //           MyListener myListener=null;  
    128. //          if (convertView == null) {  
    129. //                
    130. //              holder=new ViewHolder();    
    131. //                
    132. //              //可以理解为从vlist获取view  之后把view返回给ListView  
    133. //               myListener=new MyListener(position);  
    134. //                   
    135. //              convertView = mInflater.inflate(R.layout.vlist, null);  
    136. //              holder.title = (TextView)convertView.findViewById(R.id.title);  
    137. //              holder.info = (TextView)convertView.findViewById(R.id.info);  
    138. //              holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);  
    139. //              convertView.setTag(holder);               
    140. //          }else {               
    141. //              holder = (ViewHolder)convertView.getTag();  
    142. //          }         
    143. //            
    144. //          holder.title.setText((String)mData.get(position).get("title"));  
    145. //          holder.info.setText((String)mData.get(position).get("info"));  
    146. //          holder.viewBtn.setTag(position);  
    147. //          //给Button添加单击事件  添加Button之后ListView将失去焦点  需要的直接把Button的焦点去掉  
    148. //          holder.viewBtn.setOnClickListener( myListener);  
    149. //            
    150. //          //holder.viewBtn.setOnClickListener(MyListener(position));  
    151. //                    
    152. //          return convertView;  
    153. //      }  
    154. //  }  
    155. //    
    156. //   private class MyListener implements OnClickListener{  
    157. //          int mPosition;  
    158. //          public MyListener(int inPosition){  
    159. //              mPosition= inPosition;  
    160. //          }  
    161. //          @Override  
    162. //          public void onClick(View v) {  
    163. //              // TODO Auto-generated method stub  
    164. //              Toast.makeText(ListViewActivity.this, title[mPosition], Toast.LENGTH_SHORT).show();  
    165. //          }  
    166. //            
    167. //      }  
    168. //  
    169. //    
    170.       
    171.       
    172.       
    173.       
    174.       
    175.       
    176.       
    177.       
    178.     //提取出来方便点  
    179.     public final class ViewHolder {  
    180.         public TextView title;  
    181.         public TextView info;  
    182.         public Button viewBtn;  
    183.     }  
    184.     public void showInfo(int position){  
    185.           
    186.         ImageView img=new ImageView(ListViewActivity.this);  
    187.         img.setImageResource(R.drawable.b);  
    188.         new AlertDialog.Builder(this).setView(img)  
    189.         .setTitle("详情"+position)  
    190.         .setMessage("菜名:"+title[position]+"   价格:"+info[position])  
    191.         .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
    192.             @Override  
    193.             public void onClick(DialogInterface dialog, int which) {  
    194.             }  
    195.         })  
    196.         .show();  
    197.     }  
    198.       
    199.       
    200. }  


    2.main.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <ListView   
    8.         android:id="@+id/listView"  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="fill_parent"  
    11.         android:divider="@drawable/list_line"  
    12.         android:dividerHeight="1dip" />  
    13.   
    14. </LinearLayout>  


    3.vlist.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <ListView   
    8.         android:id="@+id/listView"  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="fill_parent"  
    11.         android:divider="@drawable/list_line"  
    12.         android:dividerHeight="1dip" />  
    13.   
    14. </LinearLayout>  


    4.btn_detail_selecter.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
    3.   
    4.     <item android:drawable="@drawable/btn_detail_normal" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"/>  
    5.     <item android:drawable="@drawable/btn_detail_pressed" android:state_enabled="true" android:state_pressed="true"/>  
    6.     <item android:drawable="@drawable/btn_detail_pressed" android:state_enabled="true" android:state_focused="true"/>  
    7.   
    8. </selector>  


    5.item.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
    3.   
    4.     <item android:drawable="@drawable/item_higlight" android:state_focused="true" android:state_pressed="false"/>  
    5.     <item android:drawable="@drawable/item_higlight" android:state_focused="false" android:state_pressed="true"/>  
    6.     <item android:drawable="@drawable/item_higlight" android:state_selected="true"/>  
    7.     <item android:drawable="@drawable/item_higlight" android:state_focused="true"/>  
    8.     <item android:drawable="@drawable/item_higlight"/>  
    9.   
    10. </selector>  



    6.colors.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <resources>  
    3.     <color name="region">#8000ff00</color>  
    4.     <color name="listTitle">#ff23323b</color>  
    5.     <color name="text">#ff848f9b</color>  
    6.     <color name="write">#ffffffff</color>  
    7. </resources>  


    7.values.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.   
    4.     <string name="hello">Hello World, ListViewActivity!</string>  
    5.     <string name="app_name">ListView</string>  
    6.   
    7. </resources>  


    8.drawables.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <resources>  
    3.     <item type="drawable" name="bg">#80000000</item>  
    4.     <item type="drawable" name="transparent">#00000000</item>  
    5.     <item type="drawable" name="lightblue">#ffcfe1ed</item>  
    6.     <item type="drawable" name="readmenu_btn_bg_f">#30ffffff</item>  
    7.     <item type="drawable" name="readmenu_btn_bg_p">#50ffffff</item>  
    8.     <item type="drawable" name="blackMask">#30000000</item>  
    9. </resources>  



    需要源码的请到github  clone,方便的话Follow一下哈: https://github.com/Juneor/ListViewButtonListener

  • 相关阅读:
    Python13_安装、解释器
    Python12_关于文件概念的讨论与序列化
    Python11_文件的读写
    which | whereis |locate |find
    tail命令 | head命令
    cat 命令|more命令|less命令
    数据库模型设计,第一范式、第二范式、第三范式简单例子理解
    json
    正则表达式
    SFTP相关命令
  • 原文地址:https://www.cnblogs.com/wi100sh/p/5425713.html
Copyright © 2020-2023  润新知