8

ich mit dem Layout eine Aktivität haben, zu beheben:Wie die Position einer Ansicht in einem ViewPager und CoordinatorLayout

<android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_height="56dp" 
       android:layout_width="match_parent" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:contentInsetLeft="0dp" 
       app:contentInsetStart="0dp" 
       app:layout_collapseMode="pin"/> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       android:layout_width="wrap_content" 
       app:tabIndicatorColor="@color/indicator" 
       app:tabMode="scrollable" 
       android:layout_height="wrap_content" /> 

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

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

Das in ViewPager geladen Fragment hat ein Layout:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/refresh_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/gridView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingTop="10dp" 
      android:scrollbars="vertical" /> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/download_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="16dp" 
     android:layout_marginLeft="16dp" 
     android:src="@drawable/ic_btn_download" /> 

</RelativeLayout> 

Wie kann ich die Position (verhindern Scrollen) der FloatingActionButton innerhalb des Fragments (es scrollt mit seinen Eltern) beheben?

Beachten Sie, dass ich FloatingActionButton nicht zum Aktivitätslayout verschieben konnte.

+0

Warum konnten Sie den FloatingActionButton nicht in Ihr Aktivitätslayout verschieben? –

Antwort

3

Sie sollten ein FAB im Aktivitätslayout verwenden. Wenn Sie verschiedene Farben oder Aktionen benötigen, können Sie FAB in Ihrem Fragment definieren und die Farbe oder die Aktion für jedes Fragment festlegen.

8

Sie sollten Ihre unverankerte Aktionsschaltfläche von Ihrem Ansichtspager in das Koordinatorlayout verschieben. Von dort können Sie Ihre Floating Action-Schaltfläche Position mit

android:layout_gravity="end|bottom" 

oder

app:layout_anchor="@id/yourViewToAnchor" 
app:layout_anchorGravity="bottom|right|end" 

eingestellt Und vergessen Sie nicht, Ihre Schwimm Aktion Taste auf der untersten Ihres Koordinators Layout XML zu setzen, dass Ihr floating, um sicherzustellen, Aktionsschaltfläche am oberen Rand Ihres Layouts.

+0

Es hat für mich funktioniert. Vielen Dank! – lalosoft

+0

Können Sie mir bitte erklären, wie kann ich es funktionieren lassen? Ich möchte meine Ansicht nicht vom Fragment zum Aktivitätslayout verschieben. Bitte hilf mir. –

+0

@SagarPanwala Sie können Ihren Fragment-Wrapper als Coordinator-Layout anstelle eines anderen Layouts verwenden. Von dort aus können Sie Ihre schwimmende Aktionsschaltfläche festlegen. – HendraWD

Verwandte Themen