问题
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
回答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.
回答2:
I achieved this by setting screenOrientation to landscape, then putting the content of the viewpager as if it is in the portrait mode.
来源:https://stackoverflow.com/questions/8936327/having-vertically-scrolling-pages-in-viewpager