如何将多个 ViewPagers 放入一个 ScrollView?

How to get several ViewPagers into a ScrollView?(如何将多个 ViewPagers 放入一个 ScrollView?)
本文介绍了如何将多个 ViewPagers 放入一个 ScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个布局,其中包含一个 ViewPagers 列表.每个 ViewPager 都可以独立滑动.请参阅下面图片的链接.

I am trying to implement a layout, which contains a list of ViewPagers. Each ViewPager is swipeable independently. See the link to the picture below.

布局图片

我尝试使用 ScrollView 和一个带有 ViewPager 的 LinearLayout,但我只显示了一个 ViewPager.甚至可以在一个屏幕上获得多个 ViewPager 吗?

I tried with ScrollView and a LinearLayout with ViewPagers inside it, but I only get one ViewPager shown. Is it even possible to get several ViewPagers on one screen?

到目前为止我的代码:main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager2"
            android:layout_width="wrap_content"
            android:layout_height="100dp" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager3"
            android:layout_width="wrap_content"
            android:layout_height="100dp" />
    </LinearLayout>

</ScrollView>

MainActivity

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ViewPagerAdapter adapter2 = new ViewPagerAdapter( this );
        ViewPager pager = (ViewPager)findViewById( R.id.viewpager2 );
        pager.setAdapter( adapter2 );

        ViewPagerAdapter adapter3 = new ViewPagerAdapter( this );
        ViewPager pager3 =
            (ViewPager)findViewById( R.id.viewpager3 );
        pager3.setAdapter( adapter3 );   
    }

}

有什么想法吗?谢谢!这段代码确实有效!

Any ideas? Thanks! this code actually works!

推荐答案

在您的 LinearLayout 上,在您的 XML 中.设置 android:weightSum="2".对于每个 viewpager,设置 android:layout_weight="1".此外,将 LinearLayout 的每个子项的高度设置为 0dp.这应该可以解决问题

On your LinearLayout, in your XML. Set android:weightSum="2". For each viewpager, set android:layout_weight="1". Also, set the height of each child of the LinearLayout to 0dp. That should do the trick

这篇关于如何将多个 ViewPagers 放入一个 ScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)