0

Ich versuche, das CoordinatorLayout in meine App zu implementieren. Ich habe viele verschiedene Tutorials ausprobiert und auch versucht, die Lösung auf CoordinatorLayout + AppBarLayout + NavigationDrawer für mein Problem anzupassen, aber ich konnte nicht herausfinden, warum es nicht funktioniert.KoordinatorLayout + Fragment + NavigationDrawer

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/id_appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:fitsSystemWindows="true"> 

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

     <ImageView 
      android:id="@+id/expandedImage" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:scaleType="centerCrop" 
      android:src="@drawable/header" 
      app:layout_collapseMode="parallax" 
      app:layout_collapseParallaxMultiplier="0.7" /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar_actionbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@android:color/transparent" 
      android:minHeight="@dimen/abc_action_bar_default_height_material" 
      app:layout_scrollFlags="scroll|enterAlways" /> 
    </android.support.design.widget.CollapsingToolbarLayout> 

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

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true"> 

     <Button 
      android:id="@+id/emiter" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_gravity="center_horizontal|top" 
      android:layout_marginTop="-70dp" 
      android:text="" /> 
    </FrameLayout> 

    <fragment 
     android:id="@+id/fragment_drawer" 
     android:name="com.myapp.test.navigationdrawer.NavigationDrawerFragment" 
     android:layout_width="@dimen/navigation_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

Der Koordinator scheint sich zu arbeiten, da ich die Wirkung nur bekommen kann, aber wenn „Scrollen“, um den oberen Teil des Bildschirms mit der Symbolleiste. Es funktioniert nicht für meine ScrollView in meinem Fragment unterhalb der ToolBar. Was mache ich falsch?

+0

Was nicht funktioniert? Das ScrollView/Fragment, von dem du gesprochen hast, ist nicht Teil des Codes in der Frage? – jayeshsolanki93

+0

@ jayeshsolanki93 Ja, also die Fragmente sind anders und ich habe sie mit fragmentTransaction.replace (R.id.container, newFragment) geändert; – user754730

Antwort

1

Es funktioniert nicht für meinen Scroll in meinem Fragmente

Die CoordinatorLayout nur verschachtelte Scroll hören können. Verwenden Sie NestedScrollView oder RecyclerView als root View in jedem Fragment, das Sie Ihrer Aktivität hinzufügen, und das CoordinatorLayout wird Scroll-Ereignisse erhalten.

Wie so:

Ihre activity.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/id_appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:fitsSystemWindows="true"> 

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

     <ImageView 
      android:id="@+id/expandedImage" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:scaleType="centerCrop" 
      android:src="@drawable/test" 
      app:layout_collapseMode="parallax" 
      app:layout_collapseParallaxMultiplier="0.7" /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar_actionbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@android:color/transparent" 
      android:minHeight="@dimen/abc_action_bar_default_height_material" 
      app:layout_scrollFlags="scroll|enterAlways" /> 
    </android.support.design.widget.CollapsingToolbarLayout> 

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

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true"> 

     <Button 
      android:id="@+id/emiter" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_gravity="center_horizontal|top" 
      android:layout_marginTop="-70dp" 
      android:text="" /> 
    </FrameLayout> 

    <fragment 
     android:id="@+id/fragment_drawer" 
     class="ru.solodovnikov.test.MainFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</android.support.v4.widget.DrawerLayout> 

Ihre fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="match_parent" 
     android:text="TOOOOOO LONG TEXT..." 
     android:layout_height="wrap_content" /> 
</android.support.v4.widget.NestedScrollView> 
+0

Also kann ich es nicht für ein Fragment überhaupt verwenden? – user754730

+0

Sie können ScrollView verwenden. Aber CoordinatorLayout wird keine Scroll-Ereignisse empfangen und Ihre Ansichten darin werden nicht auf Scrollen reagieren. Ändern Sie einfach Ihre ScrollView in NestedScrollView. – Zellius

+0

Also im Grunde kann ich diesen Code nicht in meiner activity_main.xml Datei verwenden (also ich brauche es nur einmal) aber stattdessen muss ich ihn in die XML Datei jedes Fragments einfügen. – user754730