本文介绍了ListView给每个Item上面的按钮添加事件,具体如下:
1.先看下效果图:
在这里仅供测试,我把数据都写死了,根据需要可以自己进行修改,此外实现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
2.main.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
3.vlist.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
4.btn_detail_selecter.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
5.item.xml
<&#63;xml version="1.0" encoding="UTF-8"&#63;>
6.colors.xml
<&#63;xml version="1.0" encoding="UTF-8"&#63;>#8000ff00 #ff23323b #ff848f9b #ffffffff
7.values.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>Hello World, ListViewActivity! ListView
8.drawables.xml
<&#63;xml version="1.0" encoding="UTF-8"&#63;>- #80000000
- #00000000
- #ffcfe1ed
- #30ffffff
- #50ffffff
- #30000000
源码下载:源码下载地址
原文链接:http://blog.csdn.net/qq435757399/article/details/8256453
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。