具有ViewPager垂直滚动页面页面、ViewPager

2023-09-05 03:08:56 作者:立白洗脸效果好

这可能是显而易见的,并完全不必要的职位,但我已经有相当大的问题,解决的方法,以允许在VewPager页面垂直滚动功能,并且很少有决议即将跨越谷歌,甚至在这里。我发现一些自称为解决这个问题,但他们似乎进行扩展和令人费解。因此,对于那些谁可能会寻找同样的问题继承人的解决方案。有了这个(你将要作出更大的字符串比我写在这里),你就可以垂直滚动页面之间的内容和刷卡。

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

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>

滚动viewpager类

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);
    }

}
}

我愿意接受批评的code,但至少在这里是一个正常运作的例子

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

推荐答案

我通过设置screenOrientation景观,然后把viewpager的内容,就好像它是在肖像模式实现了这一点。

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