3 回答

TA貢獻1842條經驗 獲得超13個贊
這是一個非常簡化的版本,它使用兩個偵聽器(onTouch用于滑動檢測,onClickIem用于項目單擊檢測)使用isSwipe標志停止onClickItemListener,直到其確認不是滑動
在檢測到點擊
不是第一次刷卡的情況下
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
if(!isSwipe)
{
adapter.increase(arg2);
adapter.notifyDataSetChanged();
}
}
});
檢測滑動
listView.setOnTouchListener(new OnTouchListener() {
private int action_down_x = 0;
private int action_up_x = 0;
private int difference = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
action_down_x = (int) event.getX();
isSwipe=false; //until now
break;
case MotionEvent.ACTION_MOVE:
if(!isSwipe)
{
action_up_x = (int) event.getX();
difference = action_down_x - action_up_x;
if(Math.abs(difference)>50)
{
Log.d("action","action down x: "+action_down_x);
Log.d("action","action up x: "+action_up_x);
Log.d("action","difference: "+difference);
//swipe left or right
if(difference>0){
//swipe left
Log.d("action","swipe left");
adapter.decrease(selectedItem);
adapter.notifyDataSetChanged();
}
else{
//swipe right
Log.d("action","swipe right");
}
isSwipe=true;
}
}
break;
case MotionEvent.ACTION_UP:
Log.d("action", "ACTION_UP - ");
action_down_x = 0;
action_up_x = 0;
difference = 0;
break;
}
return false; //to allow the clicklistener to work after
}
})
- 3 回答
- 0 關注
- 249 瀏覽
添加回答
舉報