按钮的点击事件这样做是不行的,比如QQ空间的说说评论和点赞其实就是相当于两个按钮。一般实现这两个按钮的点击事件归结为一种思想---接口回调。具体实现的话有两种方法,一中是通过实现接口方法,一中是通过实
现抽象方法。当然接口和抽象类在java中其实差不多的东西,虽然两者有很大不同,但是他们有着一样的功能。
两种方法都有实现,其中还应当注意一般每个控件都有一个setTag()的方法,这个方法在这中问题上起着至关重要的作用。一般可以用来记录按钮的位置之类的。
抽象类的实现:
package com.example.listviewbuttonclick.adapter;
import android.view.View;
import android.view.View.OnClickListener;
public abstract class IClick implements OnClickListener {
@Override
public void onClick(View v) {
listViewItemClick((Integer) v.getTag(), v);
}
public abstract void listViewItemClick(int position, View v);
}
适配器
package com.example.listviewbuttonclick.adapter;
import java.util.List;
import com.example.listviewbuttonclick.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ContentAdapter extends BaseAdapter {
private static final String TAG = "ContentAdapter";
private List<String> mContentList;
private LayoutInflater mInflater;
private IClick mListener;
public ContentAdapter(Context context, List<String> contentList,
IClick listener) {
mContentList = contentList;
mInflater = LayoutInflater.from(context);
mListener = listener;
}
@Override
public int getCount() {
Log.i(TAG, "getCount");
return mContentList.size();
}
@Override
public Object getItem(int position) {
Log.i(TAG, "getItem");
return mContentList.get(position);
}
@Override
public long getItemId(int position) {
Log.i(TAG, "getItemId");
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(TAG, "getView");
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView
.findViewById(R.id.textView1);
holder.button = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mContentList.get(position));
holder.button.setOnClickListener(mListener);
holder.button.setTag(position);
return convertView;
}
public class ViewHolder {
public TextView textView;
public Button button;
}
}
接口实现
package com.example.listviewbuttonclick.adapter.inter;
import android.view.View;
public interface InterClick {
public void commentClick(View v);
public void shareClick(View v);
}
适配器
package com.example.listviewbuttonclick.adapter;
import java.util.List;
import com.example.listviewbuttonclick.R;
import com.example.listviewbuttonclick.adapter.inter.InterClick;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ContentAdapter extends BaseAdapter implements OnClickListener {
private static final String TAG = "ContentAdapter";
private List<String> mContentList;
private LayoutInflater mInflater;
private InterClick mCallback;
public ContentAdapter(Context context, List<String> contentList,
InterClick callback) {
mContentList = contentList;
mInflater = LayoutInflater.from(context);
mCallback = callback;
}
@Override
public int getCount() {
Log.i(TAG, "getCount");
return mContentList.size();
}
@Override
public Object getItem(int position) {
Log.i(TAG, "getItem");
return mContentList.get(position);
}
@Override
public long getItemId(int position) {
Log.i(TAG, "getItemId");
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(TAG, "getView");
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView
.findViewById(R.id.textView1);
holder.button1 = (Button) convertView.findViewById(R.id.button1);
holder.button2 = (Button) convertView.findViewById(R.id.button2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mContentList.get(position));
holder.button1.setOnClickListener(this);
holder.button2.setOnClickListener(this);
// 设置位置,获取点击的条目按钮
holder.button1.setTag(position);
holder.button2.setTag(position);
return convertView;
}
public class ViewHolder {
public TextView textView;
public Button button1;
public Button button2;
}
// 响应按钮点击事件,调用子定义接口,并传入View
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
mCallback.commentClick(v);
break;
case R.id.button2:
mCallback.shareClick(v);
break;
default:
break;
}
}
}
这两种分别是不同的方法实现的按钮点击事件,具体实现都在源码中