5

私は、ViewPagerの中にあるFragment内のAndroid Design Support Libraryからフローティングアクションボタンを取得しようとしています。私は4つのタブを持っていて、タブの1つのみにFABが必要です。次のように私のレイアウトは以下のとおりです。Android Design Support Library FAB ViewPagerのコーディネーターレイアウトのフラグメント

main_layout.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/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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="?attr/colorPrimary" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:layout_scrollFlags="scroll|enterAlways" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     app:tabIndicatorHeight="3dp" 
     android:layout_width="match_parent" 
     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" /> 

list_fragment_with_fab.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:padding="5dip" 
android:background="@color/Transparent_White" 
android:fitsSystemWindows="false" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<org.lucasr.twowayview.widget.TwoWayView 
    android:id="@+id/clip_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:twowayview_layoutManager="ListLayoutManager" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/add_list_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:layout_margin="16dp" 
    android:src="@drawable/ic_list_add" 
    app:layout_anchor="@+id/clip_recycler_view" 
    app:layout_anchorGravity="bottom|right|end" /> 

さて問題は、ありますFABは設計仕様通り動作しません。ファブの隠蔽と表示は機能しません。また、FABはフラグメントがアクティブ化されたときの最初の場所ではありません。私はそれをより明確にするために以下のスクリーンショットを添付しました。

ご覧のとおり、FABは画面外です。スクロールするとツールバーが非表示になり(Playストアアプリと思う)、タブが残っているとFABが上にスクロールします。これは、設計サポートライブラリのバグ

enter image description here

ですか?またはレイアウトが間違っていますか?また、私はFABをフラグメントの1つにしか入れたくないので、main_layout.xmlに追加するとその目的を破ることができます。

+0

あなたは、あなたのレイアウトでgone' 'に可視性を設定でき、これは私が別のプロジェクトで使用されるものですあなたの断片のfab.setVisibility(View.VISIBLE) 'を設定してfab – hornet2319

答えて

10

これは厳密にはバグではなく、実装したばかりの方法です。

それは、このためです:AppBarLayoutが展開されたときに

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

この動作はViewPagerのサイズを操作していない、それだけで、画面の一番下にそれをプッシュします。

あなたのフラグメントはViewPagerのサイズ全体を満たしているので、FABをViewPagerコンテナの右下に正しく配置します。 ViewPagerコンテナはオフセットされているため、右下はオフスクリーンです。この文脈で

FloatingActionButtonを使用しての「正しい」方法は、それを表示する活性を有することである - このように:

<?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:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="bubblebearapps.co.uk.nfcapi.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 
     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

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

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

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

その後、表示されるページに基づいてFloatingActionButtonのサイズ、アイコンやonClickBehaviourを変更しますOnPageChangeListenerインターフェイスを使用するViewPagerで

RecyclerViewをスコールしているときにFloatingActionButtonを下からスクロールさせるには、ビヘイビアを作成する必要があります。アプリを使ってFloatingActionButtonに

public class ScrollOffBottomBehaviour extends CoordinatorLayout.Behavior<View> { 

    private int mViewHeight; 
    private ObjectAnimator mAnimator; 

    public ScrollOffBottomBehaviour(Context context, AttributeSet attrs) { 
     super(); 
    } 

    @Override 
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) { 

     mViewHeight = child.getHeight(); 

     return super.onLayoutChild(parent, child, layoutDirection); 
    } 

    @Override 
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, 
      View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { 


     if(mAnimator == null || !mAnimator.isRunning()){ 
      int totalScroll = (dyConsumed + dyUnconsumed); 

      int targetTranslation = totalScroll > 0 ? mViewHeight : 0; 

      mAnimator = ObjectAnimator.ofFloat(child, "translationY", targetTranslation); 
      mAnimator.start(); 
     } 
    } 

    @Override 
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) { 

     return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; 


    } 


} 

設定されたこの:layout_behaviourとすべてがうまくなければなりません...

+0

ありがとうジョーを表示してください。あなたが書いたのと同じようにレイアウトに変更を加えました。アクティビティでは、選択したページに基づいてFAB show()およびhide()メソッドを処理しています。ただし、ViewPagerでRecyclerViewをスクロールすると、FABは非表示になりません。何か案は? – PsyGik

+0

私はあなたのために私の答えに追加 – JoeyJubb

+0

おかげで再びジョー。それは完全に動作します! – PsyGik

関連する問題