我一直在尝试添加一个longClickListener到WearableListView
这是我目前所做的:
listView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(getActivity(), "Long", Toast.LENGTH_SHORT).show();
return false;
}
});
这里的listView是一个weararabelistview。当我长按时,函数没有被调用。我明白,对于单次点击,我们必须设置listView.setClickListener()
而不是listView.setOnClickListener()
,是否有类似的方法来长点击?
谢谢!
尝试使用onItemLongClick代替LongClick
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
return true;
}
});
刚刚看到这个。我的破解方案是扩展wearableListView并重写它的onTouch方法来查看是否有一个长触摸被完成。还要确保触摸发生在中心项目上。我必须在项目视图中添加一个居中布尔值。
@Override
public boolean onTouchEvent(MotionEvent event) {
if(!this.isEnabled()) {
return false;
} else {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
moved = false;
timestamp = System.currentTimeMillis();
initialTouch = event.getRawX();
//handler.postDelayed(mLongPressed, 1000);
break;
case MotionEvent.ACTION_UP:
//handler.removeCallbacks(mLongPressed);
if (!moved && timestamp + 1000 < System.currentTimeMillis()) {
Rect rect;
for(int i = 0;i<getChildCount();i++){View v = getChildAt(i);
if(v instanceof WearableListItemSensor){
if(((WearableListItemSensor) v).isCenter) {
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
if(rect.contains( (int) event.getX(), (int) event.getY())){
//long pressed center
if (mLongClickListener != null)
mLongClickListener.onLongClick((int)v.getTag());
}
return true;
}
}
}
}
break;
case MotionEvent.ACTION_MOVE:
//handler.removeCallbacks(mLongPressed);
if(Math.abs(event.getRawX() - initialTouch) >= 2)
moved=true;
break;
}
return super.onTouchEvent(event);