8

私は3つのタブを持つアクティビティを持っています。各タブページは、RecyclerViewを表示するフラグメントです。フラグメントの1つにFloatingActionButtonがあります。私はこのボタンをフラグメントのレイアウトに実装しています。また、フラグメントの右下に静的にしています。ViewPagerスワイプでFloatingActionButtonを表示/非表示

フラグメントレイアウト:活動レイアウトで

- CoordinatorLayout 
    - RecyclerView 
    - FAB (without any behavior) 

、私が持っている:

- CoordinatorLayout 
    - AppBarLayout 
     - Toolbar 
     - TabLayout (SmartTabLayout) 
    - ViewPager 

問題になりましたFABは、ツールバーが展開されたときに視界から半隠されたが、完全にときに表示されていますツールバーが折りたたまれています。アクティビティ自体にFABボタンを実装すると、これは起こりませんが。しかし、私はすべての断片にボタンを持っていたくない。私は最初の断片に入れるだけです。

ここで私はこれをより明確に説明しました。

Fab hidden

活動のレイアウトのための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/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 
      android:background="@color/color_primary" 
      app:layout_scrollFlags="scroll|enterAlways" /> 

     <com.ogaclejapan.smarttablayout.SmartTabLayout 
      android:id="@+id/viewpagertab" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/tab_height" 
      android:background="@color/color_primary" /> 

    </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> 

は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" 
    android:id="@+id/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/card_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_plus_white_48dp" 
     app:layout_anchor="@id/card_list" 
     app:layout_anchorGravity="bottom|right|end" /> 

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

私の質問はrecyclerviewがスクロールされたときにボタンが表示されたままになりますように、私が作るのですかどのようです?

答えて

4

FloatingActionButtonをアクティビティに配置し、ViewPager.OnPageChangeListenershow()hide()を呼び出すことをお勧めします。このようにしてに準拠したnice enter/exit animationsが得られます。

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

    @Override 
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

    } 

    @Override 
    public void onPageSelected(int position) { 
     if (position == 0) { 
      fabAdd.show(); 
     } else { 
      fabAdd.hide(); 
     } 
    } 

    @Override 
    public void onPageScrollStateChanged(int state) { 

    } 
}); 

結果:

enter image description here

+1

私はあなたのソリューションをより多くの機能を提供するように使うつもりだと思います。あなたの提案をありがとう! – AimanB

+1

@AimanBもう1つのことは、サポートデザインライブラリの最新バージョンを使用してください。最新のバージョンでは、show()およびhide()メソッドで表示されるデフォルトのアニメーションが多数修正されています。 –

1

もRecyclerViewに

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

を追加します。

+0

これはまったく影響はありません。 – AimanB

関連する問題