1

I'm trying to make a viewpager that has editable text, but I don't want the viewpager to scroll when a text box is being edited. I'm trying to disable the scrolling by using the beginFakeDrag and endFakeDrag functions of the viewpager which causes the viewpager to ignore all touch events. For some reason if you call beginFakeDrag followed by endFakeDrap it works fine, but when separated it throws the following error:

03-08 17:54:00.511: E/AndroidRuntime(31362): FATAL EXCEPTION: main
03-08 17:54:00.511: E/AndroidRuntime(31362): java.lang.NullPointerException
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.support.v4.view.ViewPager.endFakeDrag(ViewPager.java:1683)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at gtg.gis.RequestActivity$2.onFocusChange(RequestActivity.java:115)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.view.View.onFocusChanged(View.java:2742)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.widget.TextView.onFocusChanged(TextView.java:7080)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.view.View.clearFocus(View.java:2639)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.view.ViewGroup.clearFocus(ViewGroup.java:522)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.view.View.setFlags(View.java:4645)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.view.View.setVisibility(View.java:3116)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at gtg.gis.RequestActivity$1.onClick(RequestActivity.java:95)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.view.View.performClick(View.java:2485)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.view.View$PerformClick.run(View.java:9089)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.os.Handler.handleCallback(Handler.java:587)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.os.Looper.loop(Looper.java:130)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at android.app.ActivityThread.main(ActivityThread.java:3806)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at java.lang.reflect.Method.invokeNative(Native Method)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at java.lang.reflect.Method.invoke(Method.java:507)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-08 17:54:00.511: E/AndroidRuntime(31362):    at dalvik.system.NativeStart.main(Native Method)

Here is my code.

        commentBox.setOnFocusChangeListener(new EditText.OnFocusChangeListener()
        {

            public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                    InputMethodManager mgr = (InputMethodManager) getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                    mgr.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
                    _viewPager.beginFakeDrag();
                    return;
                }

                _viewPager.endFakeDrag();
                InputMethodManager mgr = (InputMethodManager) getContext().getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        });
Pinglebon
  • 21
  • 1
  • 5
  • Previous StackOverflow entry aboutdisabling a ViewPager: http://stackoverflow.com/questions/7814017/disable-viewpager. – Brad Mar 09 '12 at 14:27

2 Answers2

5

Please check out my answer to another question about disabling the swiping of ViewPager: How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

I'd just enhance it to have a boolean:

private boolean mDisablePaging;

Then add a getter/setter if you need it for that boolean.

Then enhance onInterceptTouchEvent:

@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
    // Don't allow swiping to switch between pages if we disabled it
    if (mDisablePaging) {
       return false;
    }

    // Otherwise, do the normal behavior 
    return super.onInterceptTouchEvent(arg0);
}

Then just set mDisablePaging to true when you detect that the user is editing the text box.

Community
  • 1
  • 1
louielouie
  • 14,881
  • 3
  • 26
  • 31
  • I did see that and used it to get my code working but it seems like a waste to make a whole new class for something that looks like it already has built in. – Pinglebon Mar 12 '12 at 21:26
  • 4
    There is so much wrong with this mentality. It's not a "whole new class", it's a subclass. It's not built-in, you were just abusing a mechanism that wasn't meant to do what you wanted it to. This is the beauty of OO; you can tweak or add functionality to pieces of code that don't already do what you want. – tad Jun 25 '12 at 20:06
1

I was searching for a solution which didn't involve extending the ViewPager but this solution produce the same issue than setting OnTouchListener and returning true when needed to block swipe : "The touch down still happens, so you can move pages by doing lots of small swipes."

See https://stackoverflow.com/a/13473683/1377145 OR https://stackoverflow.com/a/13392198/1377145

Community
  • 1
  • 1
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
  • +1 for searching for a solution which didn't involve extending the ViewPager! although it can't be done... – Mia Mar 18 '19 at 08:45