My app
schows a ListView. When swiping right or left the ListView for a the next or previous day should be loaded.
My solution
I tried to use the onFling() methode to detect a left or right swipe. It works fine if I swipe on the TextView above the ListView.
My problem
Swiping on the ListView doesn't trigger the onFling() methode. Probably because my ListView is scrollable. Any ideas on how to solve this?
OnFling()
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
GestureDetector gestureDetector;
#Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
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) {
Toast.makeText(this, "Right to Left swipe", Toast.LENGTH_SHORT).show();
result = true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//left to right swipe
Toast.makeText(this, "Left to Right swipe", Toast.LENGTH_SHORT).show();
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
This sounds like an ideal use case for a ViewPager, which is meant for swiping left and right between different instances of the same fragment. The ViewPager handles all of the gestures for you.
See this guide and this guide for examples of how to set up a ViewPager.
This solved my problem:
gestureListener = new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
return gestureDetector.onTouchEvent(event);
}
};
lv.setOnTouchListener(gestureListener);
Related
Hello,
I am trying to implement dynamic map (View) into my application and I need to make it's movement behave like Google Map has. So It has to do both, scroll and fling. (I have reasons for not using the actual Google Maps).
At First time, I handled dragging and flinging of map by it's Scroll. But it jittered, because void View.setScrollX(int) can't have parameter of float and so it isn't smooth enough.
At Second, I tried Translation for moving of the map, but even when dragging alone was working and fling alone was working, together it did strange behaviour on fling.
My question is: How to make Scroll and Fling work together well? I will be glad of every advice or snipped of code :)
This is the second solution:
ImageView img;
String msg = "My LOG";
float firstPosX = 0;
float firstPosY = 0;
int touchCount;
FlingAnimation flingAnimX;
FlingAnimation flingAnimY;
GestureDetector gestureDetector;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView);
gestureDetector = new GestureDetector(getApplicationContext(), mGestureListener);
img.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
touchCount = event.getPointerCount();
//Stops Fling, so on the new touch it dont continue
if (flingAnimX != null) {
flingAnimX.cancel();
}
if (flingAnimY != null) {
flingAnimY.cancel();
}
if (touchCount == 1) {
float deltaPosX = 0;
float deltaPosY = 0;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
firstPosX = event.getX();
firstPosY = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
deltaPosX = -firstPosX + event.getX();
deltaPosY = -firstPosY + event.getY();
img.setTranslationX(deltaPosX + img.getTranslationX());
img.setTranslationY(deltaPosY + img.getTranslationY());
}
}
gestureDetector.onTouchEvent(event);
return true;
}
});
}
GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
if (touchCount == 1) {
flingAnimX = new FlingAnimation(img, DynamicAnimation.TRANSLATION_X)
.setStartVelocity(velocityX);
flingAnimX.start();
flingAnimY = new FlingAnimation(img, DynamicAnimation.TRANSLATION_Y)
.setStartVelocity(velocityY);
flingAnimY.start();
}
return true;
}
};
I'm using GestureDetector.OnGestureListener to invoke method when user moves finger across the screen in the specified direction.
My problem - just move slightly and the method is called, I would like the user to move his finger a little more on the screen.
public class SwipeListener extends GestureDetector.SimpleOnGestureListener {
public static final int MIN_SWIPE_DISTANCE = 40;
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float xDiff = e1.getX() - e2.getX();
float yDiff = e1.getY() - e2.getY();
return resolveSwipe(xDiff, yDiff);
}
private boolean resolveSwipe(float xDist, float yDist) {
float yDistAbs = Math.abs(yDist);
float xDistAbs = Math.abs(xDist);
SwipeDirection swipeDirection;
if (yDistAbs > xDistAbs) {
if (yDistAbs < MIN_SWIPE_DISTANCE) {return false;}
swipeDirection = (yDist > 0) ? SwipeDirection.DOWN: SwipeDirection.UP;
} else {
if (xDistAbs < MIN_SWIPE_DISTANCE) {return false;}
swipeDirection = (xDist > 0) ? SwipeDirection.RIGHT: SwipeDirection.LEFT;
}
onSwipeEvent(swipeDirection);
return true;
}
private void onSwipeEvent(SwipeDirection swipeDirection) {
if (swipeDirection == SwipeDirection.UP) {
return;
}
if (swipeDirection == SwipeDirection.DOWN) {
return;
}
if (swipeDirection == SwipeDirection.LEFT) {
finger1()
return;
}
if (swipeDirection == SwipeDirection.RIGHT) {
finger1();
return;
}
}
public enum SwipeDirection {
UP, DOWN, LEFT, RIGHT
}
Is it possible? What should I change or add here?
You need to calculate as to what level of x-axis or y-axis you don't want to perform any action on swipe.
You have to declare minimum distance for swipe as private static final int SWIPE_MIN_DISTANCE = 160 may be it will help you Click here to implement it
I'm working on a project and I'm using a ImageView that is moved in onScroll, I recently implemented an onFling to detect swipe but I just saw that when I swipe the picture, the two MotionEvents from the onFling get the same X value using getX(). But that doesn't happen when doing swipe outside the view.
Why can be this happening?
Is there a way to fix it?
Thanks!
This is what I'm trying to implement:
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int SWIPE_MIN_DISTANCE = 120;
final int SWIPE_MAX_OFF_PATH = 250;
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) > Common.SWIPE_THRESHOLD_VELOCITY) {
if (Common.search_submissions_ID < Common.search_submissions.length()) {
Common.search_submissions_ID = Common.search_submissions_ID + 1;
finish();
startActivity(getIntent());
} else {
//Update
}
}
// left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > Common.SWIPE_THRESHOLD_VELOCITY) {
if (Common.search_submissions_ID > 0) {
Common.search_submissions_ID = Common.search_submissions_ID - 1;
finish();
startActivity(getIntent());
}
}
} catch (Exception e) {
}
return false;
}
PD: SWIPE_THRESHOLD_VELOCITY = 4500
I'm making a list with a linearLayout and adding TextViews depending on how many items I got in the database. Problem is that I want to be able to delete an item when I swipe left on the list. I was just wondering how to get the element (view) that you're swiping? Here's the code I got for adding a TextView.
for(int i = 0; i < nrOfTodos; i++) {
TextView v = new TextView(this);
String title = todos.get(i).getTitle();
System.out.println(title);
v.setText(title);
v.setHeight(listItemSize);
v.setAllCaps(true);
v.setTextColor(Color.parseColor("#FFD9A7"));
v.setTextSize(listItemSize/6);
v.setHorizontallyScrolling(false);
v.setMaxLines(1);
v.setEllipsize(TruncateAt.END);
v.setPadding(30, 50, 0, 0);
v.setId(i);
todoViews.add(v);
v.setOnTouchListener(new OnSwipeTouchListener(this) {
#Override
public void onSwipeLeft() {
/*
*
*
* TODO!!! NEXT PART OF MY PLAN TO WORLD DOMINATION!
*
*
*/
}
});
if(i%2 == 1) {
v.setBackgroundColor(Color.parseColor("#11FFFFFF"));
}
ll.addView(v, i);
}
and I also have this
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 50;
private static final int SWIPE_VELOCITY_THRESHOLD = 10;
#Override
public boolean onDown(MotionEvent e) {
//always return true since all gestures always begin with onDown and <br>
//if this returns false, the framework won't try to pick up onFling for example.
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
but I can't figure out how to actually get the view, get the titletext and remove that from the database (I can remove from the database, but I can't get what item was swiped ^^).
Any ideas would be greatly appreciated.
You may refer to this sample application Swipe_To_Delete
You may use ListView to list your data items which are got from the db.
Then modify the getSwipeItem() method in the MainActivity of the example, as follows.
#Override
public void getSwipeItem(boolean isRight, int position) {
[List_Of_Items].remove(position);
[Your_Adapter].notifyDataSetChanged();
}
Im having a calendar and I want to make it possible to swipe left/right and switch months.
Is there any listener to swipe left/right?
Thank you!
You can user a SimpleOnGestureListener like the example below: (obviously, replace the toast with the actual code that move to next/previous month)
public class SelectFilterActivity extends Activity implements 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 gestureDetector;
View.OnTouchListener gestureListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* ... */
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
}
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) {
Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
Then you attach it to your view:
// Do this for each view added to the grid
view.setOnClickListener(SelectFilterActivity.this);
view.setOnTouchListener(gestureListener);
SimpleOngestureListener can catch onFling ;
you can override this to catch swipes .
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) {
//do your stuff
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//do your stuff
}
} catch (Exception e)
{
// nothing
}
return false;
}