我有一个包含 ViewFlipper 的活动。ViewFlipper 包括 2 个布局,这两个布局本质上都只是 ListViews。
所以这里的想法是我有两个列表,从一个列表导航到另一个列表,我会使用水平滑动。
我有那个工作。但是,当滑动开始执行时,您的手指在哪个列表项上,该项也将被长按。
这是我拥有的相关代码:
public class MyActivity extends Activity implements OnItemClickListener, OnClickListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mCurrentScreen != SCREEN_SECONDLIST) {
mCurrentScreen = SCREEN_SECONDLIST;
mFlipper.setInAnimation(inFromRightAnimation());
mFlipper.setOutAnimation(outToLeftAnimation());
mFlipper.showNext();
updateNavigationBar();
}
}
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mCurrentScreen != SCREEN_FIRSTLIST) {
mCurrentScreen = SCREEN_FIRSTLIST;
mFlipper.setInAnimation(inFromLeftAnimation());
mFlipper.setOutAnimation(outToRightAnimation());
mFlipper.showPrevious();
updateNavigationBar();
}
}
} catch (Exception e) {
// nothing
}
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event))
return true;
else
return false;
}
ViewFlipper mFlipper;
private int mCurrentScreen = SCREEN_FIRSTLIST;
private ListView mList1;
private ListView mList2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_flipper);
mFlipper = (ViewFlipper) findViewById(R.id.flipper);
mGestureDetector = new GestureDetector(new MyGestureDetector());
mGestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
// set up List1 screen
mList1List = (ListView)findViewById(R.id.list1);
mList1List.setOnItemClickListener(this);
mList1List.setOnTouchListener(mGestureListener);
// set up List2 screen
mList2List = (ListView)findViewById(R.id.list2);
mList2List.setOnItemClickListener(this);
mList2List.setOnTouchListener(mGestureListener);
}
…
}
如果我改变“返回真”;来自 GestureDetector 的声明“返回 false;”,我没有得到长按。不幸的是,我经常点击。
有谁知道我该如何解决这个问题?