2016-10-15 5 views
0

私はActivityでDrawerLayoutを持ち、NavigationViewからはさまざまなフラグメントが開かれています。 フラグメントの1つにTabLayoutが含まれています。 ページがスクロールされているときにツールバーがスクロールされて(非表示になる)、 TabLayoutも非表示にする必要がありますが、これはアクティビティで実装されているサンプルがたくさんありますが、フラグメント内にTabLayoutがあり、 PlayストアのようにTabLayoutスクロールします。ここ は、私はこのXMLはコンテンツの一つであるフラグメントFragmentでTabLayoutをスクロール可能にするには?

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/fragment_container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout> 

交換/追加している

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<include 
    layout="@layout/main_screen" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/navigation_header" 
    app:menu="@menu/navigation_menu" /> 

</android.support.v4.widget.DrawerLayout> 

main_screen.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" 
tools:context=".view.activity.MainActivity"> 

<include layout="@layout/main_content" /> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#FFFF00" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#0000FF" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
</android.support.design.widget.AppBarLayout> 
</android.support.design.widget.CoordinatorLayout> 

main_content.xml activity_main.xmlレイアウトです私がスクロール可能にしたいTabLayoutを含んでいるフラグメントの1つのうちのどれか

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<android.support.design.widget.TabLayout 
    android:id="@+id/sliding_tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:tabMode="scrollable" /> 

<android.support.v4.view.ViewPager 
    android:id="@+id/view_pager" 
    android:layout_width="match_parent" 
    android:layout_height="0px" 
    android:layout_weight="1" 
    android:background="@android:color/white" /> 
</LinearLayout> 
+0

あなたが探していたものかどうか教えてください。 – Mohsen

+0

AppBarLayoutの後にする必要があります。 – Nepster

答えて

0

明らかです!

そしてとにかく

を残念ながら、あなたが何をしようとしていないか、単に公式の例を参照しようとしているかなど、あなただけちょうど最後LayoutからTabLayoutを削除し、する必要があるため、それは簡単でなければなりませんToolbarの下に置き換え、残りはそこに残してください。

最後の事は追加されます。

app:layout_scrollFlags="scroll|enterAlways" 

を別の言葉でTabLayoutスクロールまたは非表示を作るため。

So;

<?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" 
    tools:context=".view.activity.MainActivity"> 

    <include layout="@layout/main_content" /> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFF00" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="#0000FF" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/sliding_tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:tabMode="scrollable" /> 

    </android.support.design.widget.AppBarLayout> 
</android.support.design.widget.CoordinatorLayout> 
+0

あなたはmain_screenに移動する必要があります。この場合、ナビゲーションメニューから異なるフラグメントが開かれ、 TabLayout私は別の断片のためにそれを表示/非表示にする必要がありますか? –

+0

あなたの質問によると、 *フラグメントの1つにTabLayoutが含まれています。ページがスクロールすると、ツールバーがスクロールアップされます(非表示*になるので、そのビューを見つけてツールバーの下に追加するだけです)。まず、私の答えをチェックしてください。 – Mohsen

関連する問題