Android长按事件及上下文菜单

1、View.OnLongClickListener和View.OnCreateContextMenuListener均由长按视图触发。如果要长按直接触发一个事件用前者;若长按触发多个可供选择的事件,用后者。

findViewById(R.id.btn).setOnLongClickListener(new OnLongClickListener() {
	@Override
	public boolean onLongClick(View v) {
		//返回true,onCreateContextMenu将不会被触发
		return true;
	}
});
findViewById(R.id.btn).setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		//每次长按View会触发该方法然后弹出上下文菜单,onLongClick一块被触发
		menu.setHeaderTitle("haha");
		menu.add(0, 0, 0, "good").setOnMenuItemClickListener(mOnMenuItemClickListener);
		menu.add(0, 1, 0, "better").setOnMenuItemClickListener(mOnMenuItemClickListener);
		menu.add(0, 2, 0, "worse").setOnMenuItemClickListener(mOnMenuItemClickListener);
	}
});

//为上下文菜单每一项注册该监听器
private OnMenuItemClickListener mOnMenuItemClickListener = new OnMenuItemClickListener() {
	
	@Override
	public boolean onMenuItemClick(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			Log.d(TAG, "00");
			break;
		case 1:
			Log.d(TAG, "11");
			break;
		case 2:
			Log.d(TAG, "22");
			break;
		default:
			break;
		}
		return false;
	}
};
2、ListView的几个监听事件
mListView.setOnScrollListener(new OnScrollListener() {
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		Log.w(TAG, "onScrollStateChanged-"+scrollState);
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		Log.w(TAG, "onScroll-"+firstVisibleItem+"-"+visibleItemCount+"-"+totalItemCount);
	}
});
mListView.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
	@Override
	public void onSystemUiVisibilityChange(int visibility) {
		Log.w(TAG, "onSystemUiVisibilityChange-"+visibility);
	}
});
ListView的onClick已被禁掉
mListView.setOnItemClickListener(new OnItemClickListener() {
	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		Log.d(TAG, "mListView.setOnItemClickListener");
	}
});
mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		return true;
	}
});

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值