参考来源:http://www.tuicool.com/articles/bamqiq
大致思路:重写适配器,让主函数继承接口接口 ListItemClickHelp,这个接口中包含onclick方法,在主函数中实现其onclick方法后,就可以完成不同按钮的点击。参考博客中中定义了每一项的setonitemclicklistener,在跑源码的时候发现这时候点击每一项是没有什么效果的。所以换成了点击两个button来实现不同的点击事件.
1,重写适配器
package com.example.yx.pumkin;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by hp on 2016/2/20.
*/
public class ListItemClickAdapter extends BaseAdapter {
private Context contxet;
private ArrayList<String> list;
public ListItemClickHelp callback;
private LayoutInflater mInflater;
public ListItemClickAdapter(Context contxet, ArrayList<String> list,
ListItemClickHelp callback) {
this.contxet = contxet;
this.list = list;
this.callback = callback;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
mInflater = (LayoutInflater) contxet
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.list_adapter2, null);
holder = new ViewHolder();
holder.mAd_tv_show = (TextView) convertView
.findViewById(R.id.textView3);
holder.mAd_btn_one = (ImageView) convertView
.findViewById(R.id.complete_view);
holder.mAd_btn_two= (ImageView) convertView
.findViewById(R.id.read_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mAd_tv_show.setText(list.get(position));
final View view = convertView;
final int p = position;
final int one = holder.mAd_btn_one.getId();
holder.mAd_btn_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onClick(view, parent, p, one);
}
});
final int two = holder.mAd_btn_two.getId();
holder.mAd_btn_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onClick(view, parent, p, two);
}
});
return convertView;
}
public static class ViewHolder {
TextView mAd_tv_show;
ImageView mAd_btn_one;
ImageView mAd_btn_two;
}
}
2,接口 ListItemClickHelp
//这是个很神奇的接口,参数position 就是listview里面对应每一项的position. //至今不明白为什么??
package com.example.yx.pumkin;
import android.view.View;
/**
* Created by hp on 2016/2/20.
*/
public interface ListItemClickHelp {
void onClick(View item, View widget, int position, int which);
}
主函数继承这个接口后重写onclick方法,就能相应不同按钮的点击事件了
3,适配器布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="20dp"
android:id="@+id/textView3"
android:layout_marginTop="67dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/complete_view"
android:layout_toEndOf="@+id/complete_view"
android:layout_marginLeft="88dp"
android:layout_marginStart="88dp" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/complete_view"
android:background="@drawable/quan1"
android:layout_alignTop="@+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/read_view"
android:background="@color/red"
android:layout_alignTop="@+id/textView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
4,主函数
//列表显示
listString=getTaskList();
madapter = new ListItemClickAdapter(MainActivity.this,listString,(ListItemClickHelp) this);
lv1.setAdapter(madapter);
//响应不同listItem上面不同按钮的完成、查看
@Override
public void onClick(View item, View widget, int position, int which) {
switch (which) {
case R.id.complete_view:
Toast.makeText(MainActivity.this,"完成了",Toast.LENGTH_LONG).show();
break;
case R.id.read_view:
listItem=getALLlist();
HashMap one = listItem.get(position);
Intent intent = new Intent(MainActivity.this, task_read.class);
Bundle data = new Bundle();
data.putInt("_id", Integer.parseInt(String.valueOf(one.get("_id"))));
data.putString("task", String.valueOf(one.get("task")));
data.putString("date", String.valueOf(one.get("date")));
data.putString("task_type", String.valueOf(one.get("task_type")));
intent.putExtras(data);
startActivityForResult(intent, position); //标识修改项的位置
finish();
break;
default:
break;
}
}