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

转载 2018年04月17日 15:30:09

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//注意原本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主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//****************************************第二种方法,高手一般都用此种方法,具体原因,我还不清楚,有待研究
    
    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全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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

1
2
3
4
5
6
7
8
<?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

1
2
3
4
5
6
7
8
9
10
<?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

1
2
3
4
5
6
7
<?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

1
2
3
4
5
6
7
<?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

1
2
3
4
5
6
7
8
9
<?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

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

        <div class="art_xg">

您可能感兴趣的文章:


    <div class="jb51ewm"><div class="fl"><img src="//files.jb51.net/skin/2018/images/jb51ewm.png"></div><div class="fr"><p>微信公众号搜索 “ <span>脚本之家</span> ” ,选择关注</p><p>程序猿的那些事、送书等活动等着你</p></div></div></div>

为ListView每个Item上面的按钮添加事件在Adapter中响应

1.使用final变量扩展局部变量的生命周期范围主要代码 //注意原本getView方法中的int position变量是非final的,现在改为final @Override public...
  • u011895534
  • u011895534
  • 2015-12-31 09:24:36
  • 2488

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

  • 2016年07月27日 16:11
  • 333KB
  • 下载

分别使用simpleAdapter和baseAdapter为ListView的Item添加按钮响应事件

昨天在ListView Item上添加ImageButton时,发现item点击没响应,上网查找时发现有的文章认为不能使用simpleAdapter为ListView的item添加按钮响应事件,有的则...
  • a13570320979
  • a13570320979
  • 2015-07-12 15:05:26
  • 3379

给Listview里面Item里的每一个控件怎么设定点击事件

给Listview里面Item里的每一个控件怎么设定点击事件 adpter用的是继承的baseadapter,给整个listview上的Item设置点击事件就用listview.setOnItemCl...
  • li1500742101
  • li1500742101
  • 2014-03-20 11:44:16
  • 15820

ListView内 按钮的点击事件

注释写的很清楚,没啥好说的SimpleAdapter的使用public class MainActivity extends Activity { private ListView listVi...
  • Working_Harder
  • Working_Harder
  • 2015-10-04 16:25:12
  • 1349

ListView的item中Button的点击以及一些属性的处理

在ListView的item中包含Button等控件的时候,就会由于焦点的问题导致一些问题。 正常情况下(不添加一些属性的时候),点击每一个item,item是不会获得焦点的,因为在每一个item上设...
  • u012975370
  • u012975370
  • 2015-08-06 18:05:34
  • 2917

Android为ListView的item添加按钮控件

1.先看下效果图:        在这里仅供测试,我把数据都写死了,根据需要可以自己进行修改,此外实现ListView上面每个Item上的Button的事件有两种方法: 1.使用fi...
  • u010499087
  • u010499087
  • 2014-05-24 19:01:59
  • 2510

Listview每个item上加一个popupwindow

给每个item加一个popupwindow 用来操作item 看效果 1.先看看Mainactivity里 package com.example.popupwindowde...
  • lixinbb
  • lixinbb
  • 2017-08-19 08:49:59
  • 167

ListView中添加Button后,Button的点击事件和ListView的点击事件冲突

1.在ItemView配置的xml文件中的根节点添加属性android:descendantFocusability="blocksDescendants" 2.在要添加事件的控件上添加andr...
  • love_Javc_you
  • love_Javc_you
  • 2014-11-19 12:50:29
  • 6766

android捕获ListView中每个item点击事件

  • 2014年12月20日 11:44
  • 3KB
  • 下载
收藏助手
不良信息举报
您举报文章:ListView给每个Item上面的按钮添加事件
举报原因:
原因补充:

(最多只允许输入30个字)