Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having vertically scrolling pages in ViewPager

This may be obvious and a completely unneeded post but I had been having quite an issue resolving a way to allow vertical scrolling functionality on pages in a VewPager and there were very few resolutions coming across google and even here. I found some claiming to resolve the issue but they seemed to be extended and convoluted. So for those who may be searching for the same issue heres the solution. With this (and you will want to make bigger strings than what I wrote here) you will be able to vertically scroll content and swipe between pages.

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/conpageslider"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

scrolling viewpager class

public class ScrollingViewPager extends Activity{

private ViewPager pager;
private Context cxt;
private CharSequence[] pages = {"stuff", "more stuff", "other stuff"};
private PageSlider ps;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.layout);
    cxt = this;
    ps = new PageSlider();
    pager = (ViewPager) findViewById(R.id.conpageslider);
    pager.setAdapter(ps);       

}

public class PageSlider extends PagerAdapter{

    @Override
    public int getCount() {
        return pages.length;
    }

    @Override
    public Object instantiateItem(View collection, int position) {
        ScrollView sc = new ScrollView(cxt);
        sc.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        sc.setFillViewport(true);
        TextView tv = new TextView(cxt);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tv.setText(pages[position]);
        tv.setPadding(5,5,5,5);         
        sc.addView(tv);
        ((ViewPager) collection).addView(sc);

        return sc;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((ScrollView) view);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==((ScrollView)object);
    }

}
}

I am open to critique on the code but at least here is a functioning example

like image 526
Bastiat Avatar asked Jan 20 '12 03:01

Bastiat


People also ask

Is ViewPager scrollable?

In normal RecyclerView and ViewPager, we can scroll it forward and backward. However, for the first view, it cannot scroll back to the last view directly, and when it is at the last view, it cannot scroll forward to reach the first view directly.

How can I make my layout scroll vertically in Android?

Vertical scroll view going to scroll its child view in Vertical direction so we have created Linear layout as a child for Vertical scroll view and added child for linear layout.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .


2 Answers

I made similar one that using fragments in viewpager. tabWigdet can be shown or not. that example included in android-support-v4 demo application.

like image 195
lulumeya Avatar answered Sep 20 '22 18:09

lulumeya


I achieved this by setting screenOrientation to landscape, then putting the content of the viewpager as if it is in the portrait mode.

like image 43
conrad Avatar answered Sep 20 '22 18:09

conrad