Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android listview適配器裡區分不同按鈕的點擊事件

android listview適配器裡區分不同按鈕的點擊事件

編輯:關於Android編程

一、適配器 ListItemClickAdapter
public class ListItemClickAdapter extends BaseAdapter {
	private Context contxet;
	private ArrayList list;
	private ListItemClickHelp callback;
	private LayoutInflater mInflater;

	public ListItemClickAdapter(Context contxet, ArrayList 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 mdata = new ArrayList();
	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;
		}
	}

}

四、效果圖


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


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved