Android ScrollView inside ViewPager - how to make swiping more efficient
Asked Answered
H

0

7

I'm developing application where I've got ViewPager, inside of which I've got ScrollView that contains TextView. Everything is working, but when I'm trying to scroll ViewPager, I must "draw" absolutely horizontal line to swipe a page. Otherwise, if drawn line is not perfectly horizontal, it will swap ScrollView instead.

Here's image where I'm trying to explain what is my problem. Red line represents user's swipe.

enter image description here

As you can see, the last one won't swipe ViewPager, but ScrollView. And there's problem. When you are holding phone in one hand, you are using your thumb to swipe and that means you are not drawing perfectly horizontal line, but rather line that is similar to line shown on last Nexus. So my question is - how to make this swiping more efficient in order to improve user experience?

Here's some of my code that I'm using:

fragment.xml

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/divider"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp" >

</android.support.v4.view.ViewPager>

CustomPagerAdapter.java

ScrollView scroll;
TextView text;

@Override
public Object instantiateItem(ViewGroup container, int position) {
    //Adding scrollview
    scroll = new ScrollView(context);

    //Creating TextView 
    text = new TextView(context);
    text.setText(stringArray[position]);
    text.setTextAppearance(context, android.R.style.TextAppearance_Medium);

    //Adding TextView to ScrollView     
    scroll.addView(text, 0);

    //Adding ScrollView to ViewPager
    ((ViewPager) container).addView(scroll, 0);

    return scroll;
}

Fragment.java

CustomPagerAdapter adapter;
ViewPager pager;

...


    ///Set adapter
    adapter = new CustomPagerAdapter(getActivity().getApplicationContext(), stringArray);
    }
    pager.setAdapter(adapter);


    //Set onPagerChanger event
    pager.setOnPageChangeListener(new OnPageChangeListener(){


        @Override
        public void onPageScrollStateChanged(int arg0) {}
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            pager.getParent().requestDisallowInterceptTouchEvent(true);
           }

        @Override
        public void onPageSelected(int position) {
            titleUpdater(position);
            activePosition = position;
        }

    });
Hessian answered 21/7, 2014 at 13:35 Comment(1)
see #34251933Lyautey

© 2022 - 2024 — McMap. All rights reserved.