Hie all ,
is there any method in android that can differentiate the left and the right scrolling actions in android.
it is actually a customized gallery view so what I am trying to do is that on scroll actions i want to different images. I mean on left scroll I've different images and on right scroll I've different images to show. so i need to differentiate the let and right scroll.
Try this code:
new OnTouchListener() {
#Override
public boolean onTouch(View currentView, MotionEvent touchevent) {
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
if((oldTouchValue - currentX) < 50 && (oldTouchValue - currentX) > -50) {
return false;
}
if (oldTouchValue < currentX)
{
// Left swipe...
}
if (oldTouchValue > currentX)
{
// Right swipe
}
break;
}
}
return false;
}
}
Related
I have a list view and I want to make it so I can swipe each item to reveal a delete button:
I've figured out how to recognise the swipe event, I am using this code (listItem is of type View):
listItem.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
_xSwipe1 = event.getX();
break;
case MotionEvent.ACTION_UP:
_xSwipe2 = event.getX();
float deltaX = _xSwipe2 - _xSwipe1;
if (deltaX < 0)
{
Log.e("SWIPE", "Right to Left swipe");
}
else if (deltaX >0)
{
Log.e("SWIPE", "Left to right swipe");
}
break;
}
return false;
}
});
And when I swipe, I can see in the logs that the swipe event is being recognised.
However, I'm not sure how to physically make the list item start disappearing to the left?
Any help would be greatly appreciated.
If you know when the swipe is happening and how much the user has swiped you could call View.setTranslationX() depending on how much the user has swiped. Alternatively, you could forego the swipe detection logic altogether and use a ViewPager with only 2 pages. You would then override PagerAdapter.getPageWidth(...) in your PagerAdapter so that the delete button only takes up a limited amount of space.
I am trying to use ViewPager for showing different views.
Below is code I am using to check whether swiping is left or right
myPager.setOnTouchListener(new ViewPager.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
mStartDragX = x;
break;
case MotionEvent.ACTION_MOVE:
if (event.getX() > mStartDragX ) {
Log.i(TAG,"SWIPING RIGHT");
} else if (event.getX() < mStartDragX) {
Log.i(TAG,"SWIPING LEFT");
}
break;
}
return false;
}
});
So when I start to swipe from right to left or right to left its showing me correct log message.
But if I started to swipe from right to left and at the same time again if start to swipe from left to right, how can I identify whether currently swiping in which direction?
Can anyone please help ?
Thanks
Then do somethink like this..
case MotionEvent.ACTION_MOVE:
if (event.getX() > mStartDragX) {
Log.i(TAG, "SWIPING RIGHT");
mStartDragX = event.getX();
} else if (event.getX() < mStartDragX) {
Log.i(TAG, "SWIPING LEFT");
mStartDragX = event.getX();
}
Here i am saving the current position again to old position so whenever the swiped back it will give the correct result..
I have a view flipper containing two layouts with button. Now the issue is i am not able to swipe along with finger. Basically the view should swipe along with the finger. I hope you are getting what i am trying to say. Here is the onTouchevent code :-
bottomFormatFlipper = (ViewFlipper)findViewById(R.id.bottomFormatFlipper);
public boolean onTouchEvent(MotionEvent touchevent)
{
switch (touchevent.getAction())
{
// when user first touches the screen to swap
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
// if left to right swipe on screen
if (lastX < currentX)
{
// If no more View/Child to flip
if (bottomFormatFlipper.getDisplayedChild() == 0)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Left and current Screen will go OUT from Right
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_left);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_right);
// Show the next Screen
bottomFormatFlipper.showNext();
}
// if right to left swipe on screen
if (lastX > currentX)
{
if (bottomFormatFlipper.getDisplayedChild() == 1)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Right and current Screen will go OUT from Left
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_right);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_left);
// Show The Previous Screen
bottomFormatFlipper.showPrevious();
}
break;
}
}
return false;
}
From this code, when i swipe, then after a second or so, the swipe animation occurs. I want that the view should swipe with my finger
I am using the following method of flipping images: How to set dymanic images to ViewFlipper in android?
I do not want to flip using buttons. When the user touches the images or swipes the image I want to flip to the next image. Is there a way I can achieve this?
You can use ViewPager. See these links:
Implementing Horizontal View Swiping Using ViewPager and FragmentPagerAdapter in Android
Horizontal View Swiping with ViewPager, Updated
Android Viewpager as Image Slide Gallery (Swipe Gallery)
and this Library
Support Library
this might Help..... animator is a folder in res and the s_out_* is an xml file with a translate
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (viewFlipper.getDisplayedChild() == 0)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fleft);
viewFlipper.setOutAnimation(this, R.animator.s_out_right);
viewFlipper.showNext();
}
if (lastX > currentX) {
if (viewFlipper.getDisplayedChild() == 1)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fright);
viewFlipper.setOutAnimation(this, R.animator.s_out_left);
viewFlipper.showPrevious();
}
break;
}
}
return false;
}
I have a ViewPager which uses GridViews for pages. I would like the ViewPager to switch pages when I swipe across the screen.
The problem is that swipes are not detected when they are made across the GridView. Outside of the GridView, the swipes work correctly; it seems that the GridView is trapping all touch events without passing it to ViewPager first.
While fiddling with the source code, I did this to a custom class extended from GridView:
#Override
public boolean onTouchEvent(MotionEvent event) {
return pager.onInterceptTouchEvent(event);
}
-- where pager refers to the ViewPager class. With this, ViewPager will correctly detect swipes and move pages accordingly, but it doesn't allow GridView to accept any events, so I can't click on the items.
What I would like to be able to do is correctly detect swipes in ViewPager and item clicks on GridView.
I had trouble with colig's implementation, but I was able to get it to work by subclassing ViewPager and overriding the onInterceptTouchEvent() method. I only checked for swipes in the X direction to allow for vertical scrolling if necessary.
private static final int minSwipeDistance = 30;
private float mTouchX;
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean response = super.onInterceptTouchEvent(event);
float x = event.getX();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mTouchX = x;
break;
case MotionEvent.ACTION_MOVE:
float dX = Math.abs(x - mTouchX);
if (dX > minSwipeDistance)
return true;
break;
}
return response;
}
Alix is on the right track. I managed to come up with this simple-looking fix. I'm not entirely sure of how it works, but it does! And for future reference, it works for other kinds of views too -- TableLayout, for example -- not just GridView.
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
downX = x;
downY = y;
return super.onInterceptTouchEvent(event);
}
case MotionEvent.ACTION_MOVE: {
deltaX = Math.abs(downX - x);
deltaY = Math.abs(downY - y);
return super.onTouchEvent(event);
}
case MotionEvent.ACTION_UP: {
if (deltaX > 4 && deltaY > 4) {
super.onTouchEvent(event);
}
}
}
return super.onInterceptTouchEvent(event);
}
You can override onInterceptTouchEvent for dispatch evenement where you want