带有TabLayout的ViewPager不显示片段。

时间:2023-01-10 05:25:23

I am trying to create a layout like the below screenshot:

我正在尝试创建一个布局,像下面的截图:

带有TabLayout的ViewPager不显示片段。

Below is the layout XML for the same:

下面是相同的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".DetailActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/iv_movie_backdrop"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:contentDescription="@string/str_movie_backdrop_image"
                android:scaleType="fitXY" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp"

        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.CardView
                android:id="@+id/cv_poster_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:elevation="@dimen/card_elevation"
                app:cardCornerRadius="8dp"
                app:cardUseCompatPadding="true">

                <ImageView
                    android:id="@+id/iv_movie_poster"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/str_movie_poster"
                    android:padding="@dimen/poster_padding"
                    android:scaleType="fitXY"
                    android:src="@drawable/not_found" />
            </android.support.v7.widget.CardView>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@id/cv_poster_image"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_movie_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="64dp"
                    android:layout_marginStart="8dp"
                    android:padding="@dimen/poster_padding"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@android:color/black"
                    android:textStyle="bold"
                    tools:text="Spider Man and Iron Man" />

                <TextView
                    android:id="@+id/tv_release_date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:drawablePadding="@dimen/poster_padding"
                    android:drawableStart="@drawable/ic_date_range_black_24dp"
                    android:gravity="start"
                    android:padding="@dimen/poster_padding"
                    android:textAppearance="?android:textAppearanceSmall"
                    android:textColor="@android:color/black"
                    tools:text="March, 2017" />

                <TextView
                    android:id="@+id/tv_rating"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:drawablePadding="@dimen/poster_padding"
                    android:drawableStart="@drawable/ic_star_black_24dp"
                    android:gravity="start"
                    android:padding="@dimen/poster_padding"
                    android:textAppearance="?android:textAppearanceSmall"
                    android:textColor="@android:color/black"
                    tools:text="7.1" />

                <TextView
                    android:id="@+id/tv_genre"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:padding="@dimen/poster_padding"
                    android:textAppearance="?android:textAppearanceSmall"
                    android:textColor="@android:color/black"
                    android:textStyle="italic"
                    tools:text="Horror | Comedy | Drama" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cv_poster_image"
                android:orientation="vertical">

                <android.support.design.widget.TabLayout
                    android:id="@+id/sliding_tabs"
                    style="@style/CategoryTab"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabGravity="fill" />

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

            </LinearLayout>
        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_favorite_black_24dp" />

</android.support.design.widget.CoordinatorLayout>

My problem is the TabLayout with the ViewPager, I am not able to inflate the ViewPager with any FragmentLayout. If the same TabLayout and ViewPager I copy it to a new layout I am able to inflate the FragmentLayout and even the swipe works.

我的问题是使用ViewPager的TabLayout,我不能用任何碎片布局来膨胀ViewPager。如果相同的TabLayout和ViewPager我将它复制到一个新的布局中,我就可以对碎片布局,甚至是滑动操作进行膨胀。

So is it because I can only have the TabLayout and the ViewPageralone in the activity and cannot have any other views on top of them.

因为我只能在活动中使用TabLayout和ViewPageralone,不能在它们上面有任何其他视图。

2 个解决方案

#1


1  

Try adding android:fillViewport="true" to your NestedScrollView

尝试添加android:fillViewport="true"到您的NestedScrollView。

in your Xml:

在你的Xml:

<android.support.v4.widget.NestedScrollView
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fillViewport="true"   //add this
   app:layout_behavior="@string/appbar_scrolling_view_behavior">

   <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>  //change height to match parent
</android.support.v4.widget.NestedScrollView>

#2


0  

use this in your activity's oncreate()

在活动的oncreate()中使用这个

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);

and copy the below method and adapter to your activity

并将下面的方法和适配器复制到您的活动中。

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new 
ViewPagerAdapter(getSupportFragmentManager());

 //change the fragmentName as per your need

    adapter.addFragment(new FirstFragment(), "OVERVIEW");
    adapter.addFragment(new Secondfragment(), "TRAILER");
    adapter.addFragment(new thirdFragment(), "ORDERS");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }


}

#1


1  

Try adding android:fillViewport="true" to your NestedScrollView

尝试添加android:fillViewport="true"到您的NestedScrollView。

in your Xml:

在你的Xml:

<android.support.v4.widget.NestedScrollView
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fillViewport="true"   //add this
   app:layout_behavior="@string/appbar_scrolling_view_behavior">

   <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>  //change height to match parent
</android.support.v4.widget.NestedScrollView>

#2


0  

use this in your activity's oncreate()

在活动的oncreate()中使用这个

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);

and copy the below method and adapter to your activity

并将下面的方法和适配器复制到您的活动中。

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new 
ViewPagerAdapter(getSupportFragmentManager());

 //change the fragmentName as per your need

    adapter.addFragment(new FirstFragment(), "OVERVIEW");
    adapter.addFragment(new Secondfragment(), "TRAILER");
    adapter.addFragment(new thirdFragment(), "ORDERS");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }


}