10

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

3
  • 2
    this one is just to allow vertical scrolling in a horizontal viewpager. check this for a full vertical viewpager github.com/JakeWharton/Android-DirectionalViewPager Commented Mar 21, 2012 at 14:43
  • So you said that your code results in a Vertical ViewPager, but actually is just a normal, horizonatally, viepager. Am I wrong ? Commented Feb 14, 2013 at 15:27
  • use this library github.com/castorflex/VerticalViewPager Commented Nov 23, 2015 at 7:17

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

then what about all object in viewpager? it would become rotate also?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.