android listview适配器里区分不同按钮的点击事件

一、适配器 ListItemClickAdapter
public class ListItemClickAdapter extends BaseAdapter {
	private Context contxet;
	private ArrayList<String> list;
	private 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.block_list_item, null);
			holder = new ViewHolder();
			holder.mAd_tv_show = (TextView) convertView
					.findViewById(R.id.ad_tv_show);
			holder.mAd_btn_one = (Button) convertView
					.findViewById(R.id.ad_btn_one);
			holder.mAd_btn_two = (Button) convertView
					.findViewById(R.id.ad_btn_two);
			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 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 OnClickListener() {
			@Override
			public void onClick(View v) {
				callback.onClick(view, parent, p, two);
			}
		});

		return convertView;
	}

	public static class ViewHolder {
		TextView mAd_tv_show;
		Button mAd_btn_one;
		Button mAd_btn_two;
	}
}

二、接口 ListItemClickHelp

/**
 * listview点击事件接口
 * @author Alan
 *
 */
public interface ListItemClickHelp {
	void onClick(View item, View widget, int position, int which);
}

三、界面 MainActivity

public class MainActivity extends Activity implements ListItemClickHelp {

	private TextView mtv_show;
	private ListView mlv_show;
	private ArrayList<String> mdata = new ArrayList<String>();
	private ListItemClickAdapter madapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initLayout();
	}

	private void initLayout() {
		mdata.add("One");
		mdata.add("Two");
		mdata.add("Three");
		mdata.add("Four");
		mdata.add("Five");
		mdata.add("菠萝");
		mdata.add("苹果");
		mdata.add("葡萄");
		mdata.add("哈密瓜");
		mdata.add("香蕉");

		mtv_show = (TextView) this.findViewById(R.id.tv_show);
		mlv_show = (ListView) this.findViewById(R.id.lv_show);

		madapter = new ListItemClickAdapter(this, mdata, this);
		mlv_show.setAdapter(madapter);

		mlv_show.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long arg3) {
				mtv_show.setText(mdata.get(position));
			}
		});
	}

	@Override
	public void onClick(View item, View widget, int position, int which) {
		switch (which) {
		case R.id.ad_btn_one:
			mtv_show.setText(mdata.get(position) + "Btn one");
			break;
		case R.id.ad_btn_two:
			mtv_show.setText(mdata.get(position) + "Btn two");
			break;
		default:
			break;
		}
	}

}

四、效果图

android listview适配器里区分不同按钮的点击事件

五、下载地址:http://download.csdn.net/detail/shi508181017/7060961


android listview适配器里区分不同按钮的点击事件,布布扣,bubuko.com

android listview适配器里区分不同按钮的点击事件

上一篇:ios学习记录 day15


下一篇:android 反编译