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;
}
});