简体   繁体   English

ViewPager在ScrollView中不起作用

[英]ViewPager Doesn't work in ScrollView

This is my Layout where I add Tablayout and ViewPager inside the ScrollView . 这是我的布局,在ScrollView中添加Tablayout和ViewPager。

EveryThing is ok , but viewPager fragment doesn't show anything . 一切都可以,但是viewPager片段什么都没显示。

This is My xml Layout : 这是我的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>

And This is my ViewPager Adapter : 这是我的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;
    }

}

And This is Screen Shot from That : 这是屏幕截图:

在此处输入图片说明

You should add android:fillViewport="true" to your ScrollView. 您应该将android:fillViewport="true"到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 . 并且您也可以看到此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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