本文介绍的工程实现的功能是:按下列表项提示“您点击了第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
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = super.getView(position, convertView, parent); // 获取索引
final TextView text = (TextView)v.findViewById(R.id.textView2); // 定义一个文本框并关联其ID
text.setTag(position); // 以position作为索引,这样就能具体定位到某个列表项文本框
Button btn = (Button)v.findViewById(R.id.button1); // 定义一个文本框并关联其ID
btn.setTag(position); // 以position作为索引,这样就能具体定位到是哪个列表项按钮
btn.setOnClickListener(new OnClickListener() { // 添加事件监听器
public void onClick(View v) {
// TODO Auto-generated method stub
Log.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 {
@Override
protected 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();
}
});
}
}
类似的文章 点击打开链接