带按钮Listview适配器
来源:互联网 发布:介绍人工智能的小短片 编辑:程序博客网 时间:2024/06/05 00:53
本文介绍的工程实现的功能是:按下列表项提示“您点击了第x个列表项,内容为:第x个列表项”,按下列表项中的按钮,在日子面板打印“您点击了第x个按钮第x个列表项”。
许多自定义适配器都继承BaseAdapter,但对于列表项带有按钮的列表并不是最佳选择,本文介绍的便是关于列表项带有按钮的列表适配器(继承了SimpleAdapter)。
界面如下图
先从布局来看,这个界面是用两个xml文件组成,一个较为简单,就是一个ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></RelativeLayout>另一个是ListView的列表项items,里面包括提示列表项的文本框TextView(如界面中的0、1、2等),提示第几个列表项的文本框TextView(如界面中的第1个列表项、第2个列表项等),还有一个是按钮,代码如下
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:descendantFocusability="blocksDescendants" android:gravity="center_vertical" > <TextView android:id="@+id/textView1" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" android:focusable="false" /></LinearLayout>在items中的布局中应该注意两个地方,一个是父布局中的android:descendantFocusability="blocksDescendants",另一个是Button按钮中的android:focusable="false",如果你希望你的列表项可点击,按钮又可点击就需要添加这两句。
android:descendantFocusability属性是用于表示获取焦点的顺序,其属性值有三个,分别是beforeDescendants优先于其子类控件而获取焦点;afterDescendants只有当其子类控件不需要焦点时才获得焦点;blocksDescendants覆盖子类直接获得焦点。此部分内容可参考链接点击打开链接。android:focusable是表示是否获取焦点,它有两个属性值,一个是ture,表示获取焦点;而是false,不获取焦点。
接着我们来看看自定义的适配器,直接上代码
package com.sifei.lwj;import java.util.List;import java.util.Map;import android.content.Context;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.SimpleAdapter;import android.widget.TextView;public class MyAdapter extends SimpleAdapter {public MyAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) {super(context, data, resource, from, to);// TODO Auto-generated constructor stub}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView v = super.getView(position, convertView, parent);// 获取索引final TextView text = (TextView)v.findViewById(R.id.textView2);// 定义一个文本框并关联其IDtext.setTag(position);// 以position作为索引,这样就能具体定位到某个列表项文本框Button btn = (Button)v.findViewById(R.id.button1);// 定义一个文本框并关联其IDbtn.setTag(position);// 以position作为索引,这样就能具体定位到是哪个列表项按钮btn.setOnClickListener(new OnClickListener() {// 添加事件监听器public void onClick(View v) {// TODO Auto-generated method stubLog.i("Tag", "您点击了第"+v.getTag()+"个按钮"+text.getText());}});return v;}}构造方法与SimpleAdapter是一样的,主要看getView方法。主要步骤:
1、获取列表项索引;
2、定义对应的控件并关联ID;
3、设置控件的索引;
4、操作控件。
用setTag来设置索引,getTag获取索引。
在使用此自定义的适配器时与普通适配器相同
package com.sifei.lwj;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView list = (ListView)findViewById(R.id.listView1);List<Map<String, Object>> items = new ArrayList<Map<String,Object>>();for(int i=0; i<20; i++){Map<String, Object> map = new HashMap<String, Object>();map.put("i", i);map.put("name", "第"+i+"个列表项");items.add(map);}final MyAdapter adapter = new MyAdapter(MainActivity.this, items, R.layout.items, new String[]{"i","name"}, new int[]{R.id.textView1,R.id.textView2});list.setAdapter(adapter);list.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {adapter.getView(arg2, arg1, arg0);@SuppressWarnings("unchecked")Map<String, Object> map = (HashMap<String, Object>)arg0.getItemAtPosition(arg2);Toast.makeText(MainActivity.this, "您点击了第"+map.get("i").toString()+"个列表项,内容为:"+map.get("name"), Toast.LENGTH_LONG).show();}});}}
类似的文章点击打开链接
0 0
- 带按钮Listview适配器
- 带按钮的listview
- 带删除按钮的ListView
- 带删除按钮的ListView
- C# mono ListView实例带按钮事件
- 自定义一个带删除按钮的ListView
- android listview适配器里区分不同按钮的点击事件
- android listview适配器里区分不同按钮的点击事件
- android listview适配器里区分不同按钮的点击事件
- android listview适配器里区分不同按钮的点击事件
- ListView+适配器
- ListView适配器
- ListView适配器
- ListView适配器
- ListView适配器
- Listview+适配器
- listview适配器
- listview-适配器
- iOS—Xcode证书生成、设置及应用
- How to change hostname for E-Business Suite Release 12 on single node (Doc ID 1277556.1)
- 可控制的网页背景音乐
- 关于微软AjaxPro.2.dll使用的简单例子(.Net v4)
- Button响应鼠标点击事件
- 带按钮Listview适配器
- leetcode第一刷_Merge Two Sorted Lists
- 敏捷软件开发原则模式与实践语录参考
- 在.NET中调用DataWindow操作数据库
- 验证是否回文
- Correct Method of Changing the IP Address in 11i & R12 (Doc ID 751328.1)
- ams增加用户验证,实现开机自动推流。
- servlet--过滤器的使用
- hdu 3336 Count the string (DP+kmp)