I have an Activity that contains a ViewFlipper. The ViewFlipper includes 2 layouts, both of which are essentially just ListViews.
So the idea here is that I have two lists and to navigate from one to the other I would use a horizontal swipe.
I have that working. However, what ever list item your finger is on when the swipe begins executing, that item will also be long-clicked.
Here is the relevant code I have:
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);
}
…
}
If I change the "return true;" statement from the GestureDetector to "return false;", I do not get long-clicks. Unfortunately, I get regular clicks.
Does anyone know how I can get around this?
Just to throw in a completely different answer with a completely different approach...
I've made a custom view called SwipeView, it's open source etc etc blah blah blah. It should let you do what you want to do as simply as going:
mSwipeView.addChild(myList1);
mSwipeView.addChild(myList2);
You won't have to mess about with gesture detectors or anything, and the the swipe should follow your finger as you do it...
(This reads like a terrible terrible advert, please excuse me. It's been a long day ;)
Have some wonderful links:
http://jasonfry.co.uk/?id=23
http://jasonfry.co.uk/?id=24
http://jasonfry.co.uk/?id=28
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With