【问题标题】:ViewPager Doesn't work in ScrollViewViewPager 在 ScrollView 中不起作用
【发布时间】:2016-04-17 10:07:47
【问题描述】:

这是我在 ScrollView 中添加 Tablayout 和 ViewPager 的布局。

一切正常,但 viewPager 片段不显示任何内容。

这是我的 xml 布局:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#252525"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">

        <RelativeLayout
            android:id="@+id/rel"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/poster"
                android:layout_width="96dp"
                android:layout_height="128dp"
                android:layout_alignParentRight="true"
                android:layout_margin="8dp"
                android:background="#eee"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/txt_onvan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/poster"
                android:layout_marginBottom="8dp"
                android:layout_toLeftOf="@id/poster"
                android:text="عنوان" />

            <TextView
                android:id="@+id/txt_sazande"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txt_onvan"
                android:layout_marginBottom="8dp"
                android:layout_toLeftOf="@id/poster"
                android:text="سازنده" />

            <TextView
                android:id="@+id/txt_nazar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txt_sazande"
                android:layout_toLeftOf="@id/poster"
                android:text="تعداد نظر" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tablayout"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_below="@id/poster" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tablayout"
                />

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

这是我的 ViewPager 适配器:

package ir.dink.cordinatorexample;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;


public class PagerAdapter extends FragmentStatePagerAdapter {

    //============================================ Constructor
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    //============================================ GetItem Method ()
    @Override
    public Fragment getItem(int position) {
        Fragment frag = null;
        switch (position) {
            case 0:
                frag = new FragmentOne();
                break;
            case 1:
                frag = new FragmnentTwo();
                break;
        }
        return frag;
    }
    //============================================= GetCount Method ()
    @Override
    public int getCount() {
        return 2;
    }
    //============================================= GetPageTitle
    @Override
    public CharSequence getPageTitle(int position) {
        String title = "";
        switch (position) {
            case 0:
                title = "First";
                break;
            case 1:
                title = "Second";
                break;
        }
        return title;
    }

}

这是那个截图:

【问题讨论】:

    标签: android android-layout android-viewpager material-design


    【解决方案1】:

    您应该将 android:fillViewport="true" 添加到您的 ScrollView。

    喜欢这个:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#252525"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar"
            android:fillViewport="true">
    
            <RelativeLayout
                android:id="@+id/rel"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ImageView
                    android:id="@+id/poster"
                    android:layout_width="96dp"
                    android:layout_height="128dp"
                    android:layout_alignParentRight="true"
                    android:layout_margin="8dp"
                    android:background="#eee"
                    android:src="@mipmap/ic_launcher" />
    
                <TextView
                    android:id="@+id/txt_onvan"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@id/poster"
                    android:layout_marginBottom="8dp"
                    android:layout_toLeftOf="@id/poster"
                    android:text="عنوان" />
    
                <TextView
                    android:id="@+id/txt_sazande"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txt_onvan"
                    android:layout_marginBottom="8dp"
                    android:layout_toLeftOf="@id/poster"
                    android:text="سازنده" />
    
                <TextView
                    android:id="@+id/txt_nazar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txt_sazande"
                    android:layout_toLeftOf="@id/poster"
                    android:text="تعداد نظر" />
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tablayout"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_below="@id/poster" />
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/tablayout"
                    />
    
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>
    

    你也可以看到这个link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-08
      • 2016-03-19
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      相关资源
      最近更新 更多