简体   繁体   中英

ViewPager Doesn't work in ScrollView

This is my Layout where I add Tablayout and ViewPager inside the ScrollView .

EveryThing is ok , but viewPager fragment doesn't show anything .

This is My xml Layout :

<?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>

And This is my ViewPager Adapter :

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

}

And This is Screen Shot from That :

在此处输入图片说明

You should add android:fillViewport="true" to your ScrollView.

like This :

<?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>

and You can see the this link too .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM