ListView使用的是自定义适配器,列表项的布局文件中含有文本和图标,实现文本区域和图标区域的点击事件。
实现思路:在自定义适配器MyArrayAdapter 类型中自定义接口和接口方法,分别设置文本区域和图标区域的OnClickListener,然后在activity界面中MyArrayAdapter实例实现这个接口。
1、listitem布局文件
<?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="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical" > <LinearLayout
android:id="@+id/listitemTextLayout"
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/listitemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="18sp"
android:textColor="#737373"
android:gravity="center_vertical"
android:padding="15dp"
/>
</LinearLayout> <LinearLayout
android:id="@+id/listitemImgLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:clickable="true">
<ImageView
android:id="@+id/listitemImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/input_delete"
android:gravity="center_vertical"
android:contentDescription="@string/app_name"
android:padding="15dp"/>
</LinearLayout> </LinearLayout>
list_item_text_image_1
2、MyOtherArrayAdapter关键代码
自定义接口和方法的代码如下:
//列表项文字区域的点击事件监听接口
public interface OnMyItemClickListener{
void OnMyItemClick(String itemInfo);
} public void setOnMyItemClickListener(OnMyItemClickListener onMyItemClickListener){
this.onMyItemClickListener = onMyItemClickListener;
} private OnMyItemClickListener onMyItemClickListener; //列表项图标区域的点击事件监听接口
public interface OnMyItemClearClickListener{
void OnMyItemClearClick(String itemInfo);
}
public void setOnMyItemClearClickListener(OnMyItemClearClickListener onMyItemClearClickListener){
this.onMyItemClearClickListener = onMyItemClearClickListener;
}
private OnMyItemClearClickListener onMyItemClearClickListener;
使用接口代码如下:
final TextView itemText = holder.listitemText; //列表项文字区域的点击事件
holder.listitemTextLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) {
if(onMyItemClickListener != null){
String itenTitle = itemText.getText().toString();
Log.v("MyArrayAdapter", itenTitle);
onMyItemClickListener.OnMyItemClick(itenTitle);
}
}
}); //列表项图标区域的点击事件 holder.listitemImgLayout.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if(onMyItemClearClickListener != null){
String itenTitle = itemText.getText().toString();
Log.v("MyArrayAdapter", itenTitle);
onMyItemClearClickListener.OnMyItemClearClick(itenTitle);
}
}
});
3、Activity界面的使用代码如下:
private MyOtherArrayAdapter myarrayAdapter;//自定义的历史记录列表的数组适配器 //实现自定义适配器的监听接口
private void initMyAdapterListener(){ if(myarrayAdapter != null){
//列表项文字区域的点击事件
myarrayAdapter.setOnMyItemClickListener(new OnMyItemClickListener() { @Override
public void OnMyItemClick(String itemInfo) {
//赋值给AutoCompleteTextView
autoTextView.setText(itemInfo);
//搜索输入框获取焦点
autoTextView.setFocusable(true);//view是否具有接受焦点的资格
autoTextView.setFocusableInTouchMode(true);//对应在触摸模式下,设置是否有焦点来响应点触的资格
autoTextView.requestFocus();//获取焦点
autoTextView.requestFocusFromTouch();//对应在触摸模式下,获取焦点 autoTextView.setSelection(itemInfo.length());//光标移动到文字后面 }
});
//列表项图标区域的点击事件
myarrayAdapter.setOnMyItemClearClickListener(new OnMyItemClearClickListener() { @Override
public void OnMyItemClearClick(String itemInfo) {
cleanOneHistory(itemInfo);
}
});
} }
完整的自定义适配器类代码:
package com.why.autosearch.adapter; import java.util.List; import com.why.autosearch.activity.R;
import com.why.autosearch.adapter.MyArrayAdapter.ViewHolder; import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; public class MyOtherArrayAdapter extends ArrayAdapter<String>{ private int listitemResourceid;//列表项的布局文件ID
private Context mcontext; public MyOtherArrayAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
listitemResourceid = resource;
mcontext = context;
} /**
* 重写getView*/
public View getView(int position, View convertView, ViewGroup parent) { String listItemStr = getItem(position); View view = null;
ViewHolder holder = null; if(convertView == null){
view = LayoutInflater.from(mcontext).inflate(listitemResourceid, parent, false);
holder = new ViewHolder();
holder.listitemText = (TextView) view.findViewById(R.id.listitemText);
holder.listitemImg = (ImageView) view.findViewById(R.id.listitemImg); holder.listitemTextLayout = (LinearLayout) view.findViewById(R.id.listitemTextLayout);
holder.listitemImgLayout = (LinearLayout) view.findViewById(R.id.listitemImgLayout); view.setTag(holder);
}else{
view = convertView;
holder = (ViewHolder) view.getTag();
} //列表项的标题
holder.listitemText.setText(listItemStr); final TextView itemText = holder.listitemText; //列表项文字区域的点击事件
holder.listitemTextLayout.setOnClickListener(new OnClickListener() { public void onClick(View v) {
if(onMyItemClickListener != null){
String itenTitle = itemText.getText().toString();
Log.v("MyArrayAdapter", itenTitle);
onMyItemClickListener.OnMyItemClick(itenTitle);
}
}
}); //列表项图标区域的点击事件 holder.listitemImgLayout.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if(onMyItemClearClickListener != null){
String itenTitle = itemText.getText().toString();
Log.v("MyArrayAdapter", itenTitle);
onMyItemClearClickListener.OnMyItemClearClick(itenTitle);
}
}
}); return view;
} class ViewHolder{
LinearLayout listitemTextLayout;
LinearLayout listitemImgLayout; TextView listitemText;
ImageView listitemImg;
} //列表项文字区域的点击事件监听接口
public interface OnMyItemClickListener{
void OnMyItemClick(String itemInfo);
} public void setOnMyItemClickListener(OnMyItemClickListener onMyItemClickListener){
this.onMyItemClickListener = onMyItemClickListener;
} private OnMyItemClickListener onMyItemClickListener; //列表项图标区域的点击事件监听接口
public interface OnMyItemClearClickListener{
void OnMyItemClearClick(String itemInfo);
}
public void setOnMyItemClearClickListener(OnMyItemClearClickListener onMyItemClearClickListener){
this.onMyItemClearClickListener = onMyItemClearClickListener;
}
private OnMyItemClearClickListener onMyItemClearClickListener;
}
MyOtherArrayAdapter
效果图: