2

我想創建像下面的截圖佈局的片段:ViewPager與TabLayout不顯示

Layout Screenshot

下面是相同的佈局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> 

我問題是ViewPager的TabLayout,我無法使用任何FragmentLayout對ViewPager充氣。如果將相同的TabLayout和ViewPager複製到新的佈局,我可以爲FragmentLayout甚至滑動工作充氣。

所以是因爲我只能在活動中使用TabLayout和ViewPageralone,並且不能在其上有任何其他視圖。在活動的OnCreate()

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

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

和下面的方法和適配器複製到你的活動

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


} 
+0

您的viewpager片段中有元素嗎? – rafsanahmad007

+0

我的片段佈局有一個文本設置爲「1」的TextView。 –

回答

1

嘗試增加android:fillViewport="true"

+0

工作,它工作,你真棒...,我不知道我是如何錯過了這一點。非常感謝。 –

+0

快樂編碼....! – rafsanahmad007

0

使用這個你NestedScrollView

在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> 
+0

我已經有一個類似的代碼,問題是ScrollView沒有擴展其內容來填充視口。需要設置android:fillViewport =「true」@ rafsanahmad007答案爲我工作。 –