欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android为ListView每个Item上面的按钮添加事件

程序员文章站 2024-03-01 19:59:22
本文介绍了listview给每个item上面的按钮添加事件,具体如下: 1.先看下效果图: 在这里仅供测试,我把数据都写死了,根据需要可以自己进行修改,此外实现li...

本文介绍了listview给每个item上面的按钮添加事件,具体如下:

1.先看下效果图:android为ListView每个Item上面的按钮添加事件
在这里仅供测试,我把数据都写死了,根据需要可以自己进行修改,此外实现listview上面每个item上的button的事件有两种方法:

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

//注意原本getview方法中的int position变量是非final的,现在改为final 
    @override 
    public view getview(final int position, view convertview, viewgroup parent) { 
       viewholder holder = null; 
      if (convertview == null) { 
         
        holder=new viewholder();  
         
        //可以理解为从vlist获取view 之后把view返回给listview 
        convertview = minflater.inflate(r.layout.vlist, null); 
        holder.title = (textview)convertview.findviewbyid(r.id.title); 
        holder.info = (textview)convertview.findviewbyid(r.id.info); 
        holder.viewbtn = (button)convertview.findviewbyid(r.id.view_btn); 
        convertview.settag(holder);        
      }else {        
        holder = (viewholder)convertview.gettag(); 
      }     
       
      holder.title.settext((string)mdata.get(position).get("title")); 
      holder.info.settext((string)mdata.get(position).get("info")); 
      holder.viewbtn.settag(position); 
      //给button添加单击事件 添加button之后listview将失去焦点 需要的直接把button的焦点去掉 
      holder.viewbtn.setonclicklistener(new view.onclicklistener() { 
         
        @override 
        public void onclick(view v) { 
          showinfo(position);          
        } 
      }); 
       
      //holder.viewbtn.setonclicklistener(mylistener(position)); 
           
      return convertview; 
    } 
  } 
   
  //提取出来方便点 
  public final class viewholder { 
    public textview title; 
    public textview info; 
    public button viewbtn; 
  } 
  public void showinfo(int position){ 
     
    imageview img=new imageview(listviewactivity.this); 
    img.setimageresource(r.drawable.b); 
    new alertdialog.builder(this).setview(img) 
    .settitle("详情"+position) 
    .setmessage("菜名:"+title[position]+"  价格:"+info[position]) 
    .setpositivebutton("确定", new dialoginterface.onclicklistener() { 
      @override 
      public void onclick(dialoginterface dialog, int which) { 
      } 
    }) 
    .show(); 
  } 

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

//****************************************第二种方法,高手一般都用此种方法,具体原因,我还不清楚,有待研究 
   
    public view getview(int position, view convertview, viewgroup parent) { 
       viewholder holder = null; 
       mylistener mylistener=null; 
      if (convertview == null) { 
         
        holder=new viewholder();  
         
        //可以理解为从vlist获取view 之后把view返回给listview 
         mylistener=new mylistener(position); 
           
        convertview = minflater.inflate(r.layout.vlist, null); 
        holder.title = (textview)convertview.findviewbyid(r.id.title); 
        holder.info = (textview)convertview.findviewbyid(r.id.info); 
        holder.viewbtn = (button)convertview.findviewbyid(r.id.view_btn); 
        convertview.settag(holder);        
      }else {        
        holder = (viewholder)convertview.gettag(); 
      }     
       
      holder.title.settext((string)mdata.get(position).get("title")); 
      holder.info.settext((string)mdata.get(position).get("info")); 
      holder.viewbtn.settag(position); 
      //给button添加单击事件 添加button之后listview将失去焦点 需要的直接把button的焦点去掉 
      holder.viewbtn.setonclicklistener( mylistener); 
       
      //holder.viewbtn.setonclicklistener(mylistener(position)); 
           
      return convertview; 
    } 
  } 
   
   private class mylistener implements onclicklistener{ 
      int mposition; 
      public mylistener(int inposition){ 
        mposition= inposition; 
      } 
      @override 
      public void onclick(view v) { 
        // todo auto-generated method stub 
        toast.maketext(listviewactivity.this, title[mposition], toast.length_short).show(); 
      } 
       
    } 
 
   
  //提取出来方便点 
  public final class viewholder { 
    public textview title; 
    public textview info; 
    public button viewbtn; 
  } 

3.全部代码

1.listviewactivity.java全部代码:

package ms.listview; 
 
import java.util.arraylist; 
import java.util.hashmap; 
import java.util.list; 
import java.util.map; 
 
import android.app.activity; 
import android.app.alertdialog; 
import android.content.context; 
import android.content.dialoginterface; 
import android.os.bundle; 
import android.view.layoutinflater; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.view.viewgroup; 
import android.widget.adapterview; 
import android.widget.adapterview.onitemselectedlistener; 
import android.widget.baseadapter; 
import android.widget.button; 
import android.widget.imageview; 
import android.widget.listview; 
import android.widget.textview; 
import android.widget.toast; 
 
public class listviewactivity extends activity { 
  /** called when the activity is first created. */ 
  private list<map<string, object>> mdata; 
  private int flag; 
  public static string title[]=new string[]{"菜名0","菜名1","菜名2","菜名3","菜名4","菜名5","菜名6","菜名7","菜名8","菜名9"}; 
  public static string info[]=new string[]{ "¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28","¥:28",}; 
   
 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
    mdata = getdata(); 
    listview listview = (listview) findviewbyid(r.id.listview); 
    myadapter adapter = new myadapter(this); 
    listview.setadapter(adapter); 
     
     
  } 
 
 
 
  //获取动态数组数据 可以由其他地方传来(json等) 
  private list<map<string, object>> getdata() { 
    list<map<string, object>> list = new arraylist<map<string, object>>(); 
for(int i=0;i<title.length;i++){ 
    map<string, object> map = new hashmap<string, object>(); 
    map.put("title", title[i]); 
    map.put("info", info[i]); 
    list.add(map); 
} 
 
    return list; 
  } 
 
  public class myadapter extends baseadapter { 
 
    private layoutinflater minflater; 
 
    public myadapter(context context) { 
      this.minflater = layoutinflater.from(context); 
    } 
 
    @override 
    public int getcount() { 
      // todo auto-generated method stub 
      return mdata.size(); 
    } 
 
    @override 
    public object getitem(int position) { 
      // todo auto-generated method stub 
      return null; 
    } 
 
    @override 
    public long getitemid(int position) { 
      // todo auto-generated method stub 
      return 0; 
    } 
    //****************************************final方法 
//注意原本getview方法中的int position变量是非final的,现在改为final 
    @override 
    public view getview(final int position, view convertview, viewgroup parent) { 
       viewholder holder = null; 
      if (convertview == null) { 
         
        holder=new viewholder();  
         
        //可以理解为从vlist获取view 之后把view返回给listview 
         
        convertview = minflater.inflate(r.layout.vlist, null); 
        holder.title = (textview)convertview.findviewbyid(r.id.title); 
        holder.info = (textview)convertview.findviewbyid(r.id.info); 
        holder.viewbtn = (button)convertview.findviewbyid(r.id.view_btn); 
        convertview.settag(holder);        
      }else {        
        holder = (viewholder)convertview.gettag(); 
      }     
       
      holder.title.settext((string)mdata.get(position).get("title")); 
      holder.info.settext((string)mdata.get(position).get("info")); 
      holder.viewbtn.settag(position); 
      //给button添加单击事件 添加button之后listview将失去焦点 需要的直接把button的焦点去掉 
      holder.viewbtn.setonclicklistener(new view.onclicklistener() { 
         
        @override 
        public void onclick(view v) { 
          showinfo(position);          
        } 
      }); 
       
      //holder.viewbtn.setonclicklistener(mylistener(position)); 
           
      return convertview; 
    } 
  } 
    //****************************************第二种方法,高手一般都用此种方法,具体原因,我还不清楚,有待研究 
   
//   public view getview(int position, view convertview, viewgroup parent) { 
//      viewholder holder = null; 
//      mylistener mylistener=null; 
//     if (convertview == null) { 
//        
//       holder=new viewholder();  
//        
//       //可以理解为从vlist获取view 之后把view返回给listview 
//        mylistener=new mylistener(position); 
//          
//       convertview = minflater.inflate(r.layout.vlist, null); 
//       holder.title = (textview)convertview.findviewbyid(r.id.title); 
//       holder.info = (textview)convertview.findviewbyid(r.id.info); 
//       holder.viewbtn = (button)convertview.findviewbyid(r.id.view_btn); 
//       convertview.settag(holder);        
//     }else {        
//       holder = (viewholder)convertview.gettag(); 
//     }     
//      
//     holder.title.settext((string)mdata.get(position).get("title")); 
//     holder.info.settext((string)mdata.get(position).get("info")); 
//     holder.viewbtn.settag(position); 
//     //给button添加单击事件 添加button之后listview将失去焦点 需要的直接把button的焦点去掉 
//     holder.viewbtn.setonclicklistener( mylistener); 
//      
//     //holder.viewbtn.setonclicklistener(mylistener(position)); 
//          
//     return convertview; 
//   } 
// } 
//  
//  private class mylistener implements onclicklistener{ 
//     int mposition; 
//     public mylistener(int inposition){ 
//       mposition= inposition; 
//     } 
//     @override 
//     public void onclick(view v) { 
//       // todo auto-generated method stub 
//       toast.maketext(listviewactivity.this, title[mposition], toast.length_short).show(); 
//     } 
//      
//   } 
// 
//  
   
   
   
   
   
   
   
   
  //提取出来方便点 
  public final class viewholder { 
    public textview title; 
    public textview info; 
    public button viewbtn; 
  } 
  public void showinfo(int position){ 
     
    imageview img=new imageview(listviewactivity.this); 
    img.setimageresource(r.drawable.b); 
    new alertdialog.builder(this).setview(img) 
    .settitle("详情"+position) 
    .setmessage("菜名:"+title[position]+"  价格:"+info[position]) 
    .setpositivebutton("确定", new dialoginterface.onclicklistener() { 
      @override 
      public void onclick(dialoginterface dialog, int which) { 
      } 
    }) 
    .show(); 
  } 
   
   
} 

2.main.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <listview  
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:divider="@drawable/list_line" 
    android:dividerheight="1dip" /> 
 
</linearlayout>

  3.vlist.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <listview  
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:divider="@drawable/list_line" 
    android:dividerheight="1dip" /> 
 
</linearlayout> 

4.btn_detail_selecter.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 
  <item android:drawable="@drawable/btn_detail_normal" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"/> 
  <item android:drawable="@drawable/btn_detail_pressed" android:state_enabled="true" android:state_pressed="true"/> 
  <item android:drawable="@drawable/btn_detail_pressed" android:state_enabled="true" android:state_focused="true"/> 
 
</selector> 

5.item.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 
  <item android:drawable="@drawable/item_higlight" android:state_focused="true" android:state_pressed="false"/> 
  <item android:drawable="@drawable/item_higlight" android:state_focused="false" android:state_pressed="true"/> 
  <item android:drawable="@drawable/item_higlight" android:state_selected="true"/> 
  <item android:drawable="@drawable/item_higlight" android:state_focused="true"/> 
  <item android:drawable="@drawable/item_higlight"/> 
 
</selector> 

6.colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <color name="region">#8000ff00</color> 
  <color name="listtitle">#ff23323b</color> 
  <color name="text">#ff848f9b</color> 
  <color name="write">#ffffffff</color> 
</resources> 

7.values.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
  <string name="hello">hello world, listviewactivity!</string> 
  <string name="app_name">listview</string> 
 
</resources> 

8.drawables.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <item type="drawable" name="bg">#80000000</item> 
  <item type="drawable" name="transparent">#00000000</item> 
  <item type="drawable" name="lightblue">#ffcfe1ed</item> 
  <item type="drawable" name="readmenu_btn_bg_f">#30ffffff</item> 
  <item type="drawable" name="readmenu_btn_bg_p">#50ffffff</item> 
  <item type="drawable" name="blackmask">#30000000</item> 
</resources> 

源码下载:源码下载地址

原文链接:http://blog.csdn.net/qq435757399/article/details/8256453

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。