1.ListView自定义adapter中处理多事件
使用过ListView 的开发人员都知道,ListView 在一般情况下只能对每条记录设置一个监听事件。如果想在其中添加多个事件,就需要自定义Adapter 。
下面介绍一下如何自定义adapter 以及如何在一个Item 中绑定多个事件。
这里我们需要两个XML 文件、两个java 类。分别是存放ListView 的XML 、ListViewItem 的XML 、Activity 类和Adapter 类。
首先说说两个XML 文件。
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:padding= "10dip"
- android:orientation="vertical" >
- <ListView
- android:id="@id/android:list"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent" />
- </LinearLayout>
注意:这里的ListView 中id 使用了android 自定义的: @id/android:list
lvItem.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:descendantFocusability="blocksDescendants"
- android:padding="5dip" >
- <ImageView
- android:id="@+id/ItemImage"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="5dip" />
- <ImageButton
- android:id="@+id/ItemCloseWin"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@+id/ItemWinName"
- android:layout_alignTop="@+id/ItemWinName"
- android:layout_alignParentRight="true"
- android:background="#e0000000"
- android:focusable="false"
- android:gravity="left|center_vertical"
- android:src="@android:drawable/ic_menu_close_clear_cancel" />
- <ImageButton
- android:id="@+id/ItemEmail"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@id/ItemWinName"
- android:layout_alignTop="@id/ItemWinName"
- android:layout_toLeftOf="@id/ItemCloseWin"
- android:background="#e0000000"
- android:focusable="false"
- android:gravity="left|center_vertical"
- android:src="@android:drawable/ic_dialog_email" />
- <TextView
- android:id="@+id/ItemWinName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@id/ItemImage"
- android:layout_alignTop="@id/ItemImage"
- android:layout_toLeftOf="@id/ItemEmail"
- android:layout_toRightOf="@id/ItemImage"
- android:gravity="left|center_vertical"
- android:text="title"
- android:textSize="20dip" />
- </RelativeLayout>
接下来看看Activity
- package cn.mutil;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- * ListView item 多事件Activity
- * @author lihua
- *
- */
- public class LvWithButtonActivity extends ListActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 关联Layout中的ListView
- ListView vncListView = (ListView) findViewById(android.R.id.list);
- // 生成动态数组,加入数据
- ArrayList<HashMap<String, Object>> remoteWindowItem = new ArrayList<HashMap<String, Object>>();
- for (int i = 0; i < 10; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemImage", R.drawable.ic_launcher); // 图像资源的ID
- map.put("ItemWinName", "Window ID " + i);
- map.put("ItemEmail", android.R.drawable.ic_dialog_email);
- map.put("ItemCloseWin", android.R.drawable.ic_menu_close_clear_cancel);
- remoteWindowItem.add(map);
- }
- // 生成适配器的Item和动态数组对应的元素
- LvButtonAdapter listItemAdapter = new LvButtonAdapter(this,
- remoteWindowItem, // 数据源
- R.layout.lvitem, // ListItem对应的XML
- // 动态数组与ImageItem对应的子项
- new String[] { "ItemImage", "ItemWinName","ItemEmail", "ItemCloseWin" },
- // ImageItem的XML文件里面的一个ImageView,两个TextView ID
- new int[] { R.id.ItemImage, R.id.ItemWinName, R.id.ItemEmail, R.id.ItemCloseWin });
- vncListView.setAdapter(listItemAdapter);
- /**
- * 设置整个Item被点击的事件
- * 该事件在其他有事件的组件未被点击时触发
- */
- vncListView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapter, View view,
- int position,long arg3) {
- TextView content = (TextView)view.findViewById(R.id.ItemWinName);
- Toast.makeText(LvWithButtonActivity.this, content.getText().toString() , Toast.LENGTH_SHORT).show();
- }
- });
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- l.getItemAtPosition(position);
- }
- }
最后一个也是最重要的一个,自定义adapter
- package cn.mutil;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class LvButtonAdapter extends BaseAdapter {
- private class ButtonViewHolder {
- ImageView appIcon;
- TextView appName;
- ImageButton buttonEmail;
- ImageButton buttonClose;
- }
- private ArrayList<HashMap<String, Object>> mAppList;//用于存放传递过来显示于ListView中的 数据
- private LayoutInflater mInflater;
- private Context mContext;
- private String[] keyString;
- private int[] valueViewID;
- private ButtonViewHolder holder;
- public LvButtonAdapter(Context c,//上下文
- ArrayList<HashMap<String, Object>> appList,//绑定数据
- int resource00000,//ListView行记录layout
- String[] from,
- int[] to) {
- mAppList = appList;
- mContext = c;
- mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- keyString = new String[from.length];
- valueViewID = new int[to.length];
- System.arraycopy(from, 0, keyString, 0, from.length);
- System.arraycopy(to, 0, valueViewID, 0, to.length);
- }
- @Override
- public int getCount() {
- return mAppList.size();
- }
- @Override
- public Object getItem(int position) {
- return mAppList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- /**
- * 删除数据集中的值
- * @param position
- */
- public void removeItem(int position) {
- mAppList.remove(position);
- this.notifyDataSetChanged();
- }
- /**
- * 改变数据集中的值
- * @param position
- * @param map
- */
- public void changeItem(int position,HashMap<String, Object> map){
- mAppList.remove(position);
- mAppList.add(position, map);
- this.notifyDataSetChanged();
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView != null) {
- holder = (ButtonViewHolder) convertView.getTag();
- } else {
- convertView = mInflater.inflate(R.layout.lvitem, null);
- holder = new ButtonViewHolder();
- holder.appIcon = (ImageView) convertView
- .findViewById(valueViewID[0]);//可以将valueViewID[0]替换成R.id.xxx
- holder.appName = (TextView) convertView
- .findViewById(valueViewID[1]);
- holder.buttonEmail = (ImageButton) convertView
- .findViewById(valueViewID[2]);
- holder.buttonClose = (ImageButton) convertView
- .findViewById(valueViewID[3]);
- convertView.setTag(holder);
- }
- HashMap<String, Object> appInfo = mAppList.get(position);
- if (appInfo != null) {
- String aname = (String) appInfo.get(keyString[1]);
- int mid = (Integer) appInfo.get(keyString[0]);
- int eid = (Integer) appInfo.get(keyString[2]);
- int bid = (Integer) appInfo.get(keyString[3]);
- holder.appName.setText(aname);
- holder.appName.setOnClickListener(new LvButtonListener(position));
- holder.appIcon.setImageDrawable(holder.appIcon.getResources().getDrawable(mid));
- holder.buttonEmail.setImageDrawable(holder.buttonEmail.getResources().getDrawable(eid));
- holder.buttonEmail.setOnClickListener(new LvButtonListener(position));
- holder.buttonClose.setImageDrawable(holder.buttonClose.getResources().getDrawable(bid));
- holder.buttonClose.setOnClickListener(new LvButtonListener(position));
- }
- return convertView;
- }
- /**
- * 按钮事件监听
- * @author lihua
- *
- */
- private class LvButtonListener implements View.OnClickListener {
- private int position;
- LvButtonListener(int pos) {
- position = pos;
- }
- @Override
- public void onClick(View v) {
- int vid = v.getId();
- HashMap<String, Object> curMap = (HashMap<String, Object>)getItem(position);
- if (vid == holder.buttonClose.getId()){ //删除一行记录
- Toast.makeText(mContext,"position:"+position+",data is being Deleted", Toast.LENGTH_LONG).show();
- removeItem(position);
- //可以在这里操作数据库或更新服务端数据
- }else if(vid == holder.buttonEmail.getId()){//发送邮件
- Toast.makeText(mContext, "position:"+position+",sending email to xxx!",
- Toast.LENGTH_SHORT).show();
- //可以在这里操作数据库或更新服务端数据
- }else if(vid == holder.appName.getId()){//设置名称
- Toast.makeText(mContext, "position is "+position+", appName:"+curMap.get("ItemWinName").toString(),
- Toast.LENGTH_SHORT).show();
- curMap.put("ItemWinName", System.currentTimeMillis()+"");
- changeItem(position,curMap);
- //可以在这里操作数据库或更新服务端数据
- }
- }
- }
- }
2.用ViewHolder,对ListView进行一些性能优化
最近写Adapter写得多了,慢慢就熟悉了。
用ViewHolder,主要是进行一些性能优化,减少一些不必要的重复操作。(WXD同学教我的。)
具体不分析了,直接上一份代码吧:
public class MarkerItemAdapter extends BaseAdapter { private Context mContext = null; private List<MarkerItem> mMarkerData = null; public MarkerItemAdapter(Context context, List<MarkerItem> markerItems) { mContext = context; mMarkerData = markerItems; } public void setMarkerData(List<MarkerItem> markerItems) { mMarkerData = markerItems; } @Override public int getCount() { int count = 0; if (null != mMarkerData) { count = mMarkerData.size(); } return count; } @Override public MarkerItem getItem(int position) { MarkerItem item = null; if (null != mMarkerData) { item = mMarkerData.get(position); } return item; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (null == convertView) { viewHolder = new ViewHolder(); LayoutInflater mInflater = LayoutInflater.from(mContext); convertView = mInflater.inflate(R.layout.item_marker_item, null); viewHolder.name = (TextView) convertView.findViewById(R.id.name); viewHolder.description = (TextView) convertView .findViewById(R.id.description); viewHolder.createTime = (TextView) convertView .findViewById(R.id.createTime); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } // set item values to the viewHolder: MarkerItem markerItem = getItem(position); if (null != markerItem) { viewHolder.name.setText(markerItem.getName()); viewHolder.description.setText(markerItem.getDescription()); viewHolder.createTime.setText(markerItem.getCreateDate()); } return convertView; } private static class ViewHolder { TextView name; TextView description; TextView createTime; } }
其中MarkerItem是自定义的类,其中包含name,description,createTime等字段,并且有相应的get和set方法。
ViewHolder是一个内部类,其中包含了单个项目布局中的各个控件。
单个项目的布局,即R.layout.item_marker_item如下:
<?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"
android:padding="5dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:textSize="18sp" />
<TextView
android:id="@+id/createTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CreateTime"
android:textSize="16sp" />
</LinearLayout>
官方的API Demos中也有这个例子:
package com.example.android.apis.view中的List14:
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.apis.view; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.ImageView; import android.graphics.BitmapFactory; import android.graphics.Bitmap; import com.example.android.apis.R; /** * Demonstrates how to write an efficient list adapter. The adapter used in this example binds * to an ImageView and to a TextView for each row in the list. * * To work efficiently the adapter implemented here uses two techniques: * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary * * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by * getView(). This data structures contains references to the views we want to bind data to, thus * avoiding calls to findViewById() every time getView() is invoked. */ public class List14 extends ListActivity { private static class EfficientAdapter extends BaseAdapter { private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; public EfficientAdapter(Context context) { // Cache the LayoutInflate to avoid asking for a new one each time. mInflater = LayoutInflater.from(context); // Icons bound to the rows. mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1); mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2); } /** * The number of items in the list is determined by the number of speeches * in our array. * * @see android.widget.ListAdapter#getCount() */ public int getCount() { return DATA.length; } /** * Since the data comes from an array, just returning the index is * sufficent to get at the data. If we were using a more complex data * structure, we would return whatever object represents one row in the * list. * * @see android.widget.ListAdapter#getItem(int) */ public Object getItem(int position) { return position; } /** * Use the array index as a unique id. * * @see android.widget.ListAdapter#getItemId(int) */ public long getItemId(int position) { return position; } /** * Make a view to hold each row. * * @see android.widget.ListAdapter#getView(int, android.view.View, * android.view.ViewGroup) */ public View getView(int position, View convertView, ViewGroup parent) { // A ViewHolder keeps references to children views to avoid unneccessary calls // to findViewById() on each row. ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no need // to reinflate it. We only inflate a new View when the convertView supplied // by ListView is null. if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item_icon_text, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } // Bind the data efficiently with the holder. holder.text.setText(DATA[position]); holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); return convertView; } static class ViewHolder { TextView text; ImageView icon; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new EfficientAdapter(this)); } private static final String[] DATA = Cheeses.sCheeseStrings; }
其中布局:
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2007 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/icon" android:layout_width="48dip" android:layout_height="48dip" /> <TextView android:id="@+id/text" android:layout_gravity="center_vertical" android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" /> </LinearLayout>