[英]ViewPager with TabLayout not displaying the fragments
I am trying to create a layout like the below screenshot: 我正在尝试创建如下截图的布局:
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. 我的问题是带有ViewPager的TabLayout,我无法用任何FragmentLayout来膨胀ViewPager。 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. 如果我将相同的TabLayout和ViewPager复制到新的布局,我可以给FragmentLayout充气甚至滑动工作。
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,并且不能在它们之上拥有任何其他视图。
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>
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.