活动的每个片段中的浮动操作按钮的不同实现

SRB*_*wat 5 android android-fragments android-activity floating-action-button android-recyclerview

这是关于协调活动中浮动操作按钮的行为的问题,其中包含5个片段.

在我的应用程序中,我有一个Activity,它包含一个ViewPager和一个用FloatingActionButton定义的xml.ViewPager包含片段,每个片段内都有一个RecyclerView.On点击浮动操作按钮我实现了scrollToPosition(0).由于我在main活动的xml文件中实现了Floating ActionButton,因此scrollToPosition()仅在单击浮动操作按钮的最后一个片段中起作用.我如何克服这一点,以便在任何片段中点击浮动按钮时,回收器视图滚动到顶部?

我已经尝试在片段布局文件中实现了floatingactionbutton并且它完全正常.但是当我从一个片段滑动到另一个片段时,浮动动作按钮将会移动.所以有任何方法可以实现我在其中的浮动操作按钮.主活动xml文件,并且仍然能够在点击FloatingActionButton的每个片段中实现scrollToPosotion(0)?非常感谢任何建议.

以下是包含浮动操作按钮的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"
android:background="#f4f4f4"
tools:context=".MainActivity">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/tb_main"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="0dp"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

    <com.myapp.now.extras.SlidingTabLayout
        android:id="@+id/sl_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

    </com.myapp.now.extras.SlidingTabLayout>

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


<android.support.v4.view.ViewPager
    android:id="@+id/vp_tab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />


    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right|end"
    android:layout_marginBottom="@dimen/fab_margin_bottom"
    android:layout_marginEnd="@dimen/fab_margin_right"
    android:layout_marginRight="@dimen/fab_margin_right"
    android:clickable="true"
    android:src="@mipmap/top_scroll"
    app:backgroundTint="@color/colorFAB"
    app:borderWidth="0dp"
    app:fabSize="normal" />
Run Code Online (Sandbox Code Playgroud)

以下是片段的xml文件

<FrameLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/srl_swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:scrollbars="vertical"
        android:fadeScrollbars="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

<ProgressBar
    android:visibility="gone"
    android:layout_gravity="center"
    android:id="@+id/pbar"
    android:layout_width="40dp"
    android:layout_height="40dp" />
Run Code Online (Sandbox Code Playgroud)

以下是每个片段中浮动操作按钮的实现

 fab = (android.support.design.widget.FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
            llm.scrollToPositionWithOffset(0, 0);
        }
    });
Run Code Online (Sandbox Code Playgroud)

Meh*_*t K 4

Not sure if this'll work with SlidingTabLayout since I use ViewPager.

Have the FAB onClick call a method (e.g. onFABClick() ) in your activity and define it as:

public void onFABClick(View view) {
    if (fragments.get(mViewPager.getCurrentItem()) instanceof FragmentA) {
      if (fragmentA!= null) {
        fragmentA.fabOnClick();
      }
    } else if (fragments.get(mViewPager.getCurrentItem()) instanceof FragmentB) {
      if (fragmentB!= null) {
        fragmentB.fabOnClick();
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

Here fragments is the array that holds fragment objects. It is the array that is passed to the adapter of the ViewPager. I guess you'll have to change mViewPager.getCurrentItem() with the equivalent get index of current fragment method of SlidingTabLayout.

Lastly, the fabOnClick() method is implemented in each fragment. (You'll need to have all your fragments implement an interface (e.g. FABActionInterface) and add the method signature there.

Here you are basically catching the FAB onclick and directing it to call a method on the current fragment.