【问题标题】:Two ViewPagers inside ScrollView - second not visibleScrollView 中的两个 ViewPagers - 第二个不可见
【发布时间】:2015-07-10 08:17:09
【问题描述】:

我正在尝试在 ScrollView 中使用两个 ViewPager 创建布局。第一个已将高度设置为所需的大小,第二个应与布局的其余部分匹配。问题是第二个 ViewPager 不可见。我究竟做错了什么?这是我的布局:

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

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

            <android.support.v4.view.ViewPager
                android:id="@+id/gallery"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

            <android.support.v4.view.ViewPager
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical" />
        </LinearLayout>
    </ScrollView>

</FrameLayout>

我正在使用第二个 ViewPager 来显示片段(使用 FragmentPagerAdapter),这是我的 FragmentPagerAdapter 代码:

public class MainPagerAdapter extends FragmentPagerAdapter {
    public MainPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new FirstFragment();
        case 1:
            return new SecondFragment();
        default:
            return new FirstFragment();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }
}

FirstFragment 代码:

public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.first_step, container, false);
    }
}

first_step.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@android:color/holo_blue_light" />
    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@android:color/holo_green_light" />  
</LinearLayout>

【问题讨论】:

    标签: android android-layout android-viewpager


    【解决方案1】:

    如果您使用的是布局权重。您必须根据您的使用设置宽度或高度布局 0dp

    这为两个寻呼机提供了相等的空间 按比例给出重量,即 2:1 或 2:1 等

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <android.support.v4.view.ViewPager
                android:id="@+id/gallery"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
    
            <android.support.v4.view.ViewPager
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="vertical" />
        </LinearLayout>
    

    【讨论】:

    【解决方案2】:

    你必须定义

    android:layout_height="200dp"
    

    删除

     android:layout_weight="1"
    

    在两个viewpager都可见后不要设置android:layout_height="wrap_content"

    【讨论】:

    • Second ViewPager 显示片段(使用 FragmentPagerAdapter),其中布局很长,因此用户必须能够滚动它。
    • 所以,您只能使用垂直方向的线性布局,并在两个视图寻呼机中设置权重 1,它就像魅力一样工作
    • 当我在两个 ViewPager 中将 weight 设置为 1 并将 height 设置为 0dp 时,它们都不可见。
    • 这不可能是你的问题,而不是 viewpager 只查看并检查它兄弟
    【解决方案3】:

    改变

     <android.support.v4.view.ViewPager
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical" />
    

    <android.support.v4.view.ViewPager
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:orientation="vertical" />
    

    还有

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

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

    【讨论】:

    • 没有改变,只有第一个可见。
    • 我编辑了我的帖子。问题是您将高度为“match_parent”的 LinearLayout 放入 ScrollView。
    • 感谢您的回复。当我将第二个 ViewPager 的高度设置为某个大小时,它是可见的,但我想将它与其余的布局相匹配。
    • 问题是,它不能。 “布局的其余部分”是一个滚动视图,具有无限的高度。要求 LinearLayout 匹配父级高度与要求 linearlayout 无限高是一样的,这是不可能的。如果您希望线性布局占据所有屏幕,请移除 scrollView。
    【解决方案4】:

    在 ScollView 中设置 fillViewport=true。

    如果第二个 ViewPager 没有固定在屏幕上的高度,我认为 ScollView 没用。

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    

    【讨论】:

    • 谢谢,现在看起来没问题,但我不能滚动,你知道是什么问题吗?
    • 尝试让第二个 ViewPager 的高度更高,比如 1000dp。
    • 当我设置高度时,它是可滚动的,但我在 ViewPager 中的片段可能有不同的高度,所以我不能这样做。
    • 我不知道你为什么要使用scollview。如果滚动视图的子视图适合屏幕,则垂直滚动是无用的。
    • 但它们不适合屏幕,视图高度约为 2000dp
    【解决方案5】:
    case ViewPager is  a puzzle view,you must set the height for it,then it can
    appear in you UI Screen,gallery dont have this problem;
    why use ScrollView in this layout, i dont understand.
    but,if you use linearlayout
    

    所以你可以这样做,背景只是为了测试,你应该在你的项目中得到命运来匹配屏幕,而不仅仅是200 *2:

    layout:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
    
        <android.support.v4.view.ViewPager
                    android:id="@+id/content1"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:background="@android:color/darker_gray"
                    android:orientation="vertical" />
    
    
        <android.support.v4.view.ViewPager
                    android:id="@+id/content2"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:background="#ff0000"
                    android:orientation="vertical" />
    </LinearLayout>
    
    activity:
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.viewpage01);
            int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
            ViewPager content = (ViewPager) findViewById(R.id.content2);
            content.getLayoutParams().height = screenHeight - 200 * 2;
            content.requestLayout();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      相关资源
      最近更新 更多