One side ViewPager swiping only
Asked Answered
C

1

4

I have a ViewPager in my application and I would like to disable/allow swipe to right side in any time. Every view in the viewpager contains ListView. How can I do that? I am trying to do that in this way:

private int oldX;
private int deltaX = 0;

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_MOVE || motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        int newX = (int) motionEvent.getX();
        deltaX = oldX - newX;
        oldX = newX;
    }
    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        oldX = 0;
    }
    return deltaX < 0;        
}

But view is still going to right side a little bit. Anyone was solving the same issue?

Creel answered 5/9, 2012 at 7:50 Comment(0)
W
12

I was able to achieve one-side scrolling in ViewPager by taking its code to my application and adding the following

private boolean performDrag(float x) {
    boolean needsInvalidate = false;

    final float deltaX = mLastMotionX - x;
    mLastMotionX = x;

    // MY CODE STARTS
    if (deltaX < 0) {
        return false;
    }
    // MY CODE ENDS

    float oldScrollX = getScrollX();
    ...

It seems to work fine. The only thing You might need - take adapter code or provide Your own adapter, because some methods ViewPager uses in its code are not public from PagerAdapter.

Walkout answered 5/9, 2012 at 12:54 Comment(14)
Thanks a lot. It works! I took a whole class and used it in my project.Creel
@sandrstar, I tried copying both the ViewPager and the PagerAdapter classes to my app. But I get "Unable to convert to dalvik format" error while building my application. Should I rename these classes after copying them to my project? What package name should I use?Marshall
use Your application package. And, yes, You can rename them, just don't forget to make appropriate changes in layout files.Walkout
@SubinSebastian I copied the view pager class into my project with different name. Now I am facing the same problem like you(Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException). How you solved it ? Can you help... ThanksDecomposer
@Decomposer have You changed package names also?Walkout
@Walkout I just added sdk.buildtools=18.1.1 to my project.propertis file to solve this issue. It worked for me. Thanks man , i voted the answer upDecomposer
@Walkout which extra code does it require. I looked up PagerAdapter and ViewPager but none of these contains performDrag()Frogman
@Frogman e.g. grepcode.com/file/repository.grepcode.com/java/ext/… , I'm able to observe it in latest ViewPager code also. Please note 'by taking its code to my application and adding the following' which means that You need to take it's code and create your own widget based on it, not usage of some public API.Walkout
@Erum Fragments - developer.android.com/guide/components/fragments.html , ViewPager - developer.android.com/training/implementing-navigation/… . After that 'taking ViewPagers code to your application and adding the following...' from the answer.Walkout
@Erum nope, chats blocked by my employer.Walkout
@Erum I doubt that it could be related to this code. Probably it's completely different root-cause. Analyze Android logcat, try to debug and create new question with clear description of the issue and include your code in it.Walkout
Let us continue this discussion in chat.Kentonkentucky
@Kentonkentucky please, read the answer, it says <b>taking ViewPagers code to your application</b> so it should be ViewPager class or your class if you renamed it.Walkout
You made my day..works awesome..just have to import properly everywhere (Fragment+XML+Adapter)Winsor

© 2022 - 2024 — McMap. All rights reserved.