【发布时间】:2015-08-23 05:09:48
【问题描述】:
这是关于在有 5 个片段的 Activity 中协调浮动操作按钮的行为的问题。
在我的应用程序中,我有一个 Activity,其中包含一个 ViewPager 和一个使用 FloatingActionButton 定义的 xml。 ViewPager 包含 Fragment,每个 Fragment 内部都有一个 RecyclerView。单击浮动操作按钮时,我实现了 scrollToPosition(0)。由于我在主要活动的 xml 文件中实现了 Floating ActionButton,scrollToPosition() 仅在单击浮动操作按钮时的最后一个片段中有效。我如何克服这个问题,以便在单击任何片段中的浮动按钮时,回收器视图滚动到顶部?
我已经尝试在片段布局文件中实现浮动操作按钮,它工作得很好。但是这个问题是当我从一个片段滑动到另一个片段时浮动操作按钮会移动。所以有什么方法可以实现主活动xml文件中的floatingactionbutton并且仍然能够在单击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" />
以下是一个片段的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" />
以下是每个fragment中浮动动作按钮的实现
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);
}
});
【问题讨论】:
-
请提供一些代码
-
你可以在这里查看我的答案:stackoverflow.com/a/33586571/2023280
-
@RBhagwat 你解决了这个问题吗?
标签: android android-fragments android-activity android-recyclerview floating-action-button