[英]Using Floating Action Button, Swipe Refresh Layout and Coordinator Layout and Toolbar Layout Behavior together
I have a tabbed activity.我有一个标签式活动。 There are two tabs.有两个选项卡。 The only difference between tab 1 and tab 2 is, tab 1 has floating action button, tab 2 hasn't got. tab 1 和 tab 2 的唯一区别是,tab 1 有浮动操作按钮,tab 2 没有。
in Tab 1:在表 1 中:
in Tab 2:在表 2 中:
Tab 2 works great, no problem on tab 2. But tab 1 doesn't work properly.选项卡 2 效果很好,选项卡 2 没有问题。但选项卡 1 无法正常工作。 When scrolling down the recycler view, the toolbar doesn't disappear.向下滚动回收器视图时,工具栏不会消失。 When scrolling up the recycler view, the toolbar doesn't appear.向上滚动回收器视图时,不会出现工具栏。 In addition, tab 1 doesn't show all of the page.此外,选项卡 1 并未显示所有页面。 There are 40 items in recycler view, but tab 1 shows 39 items.回收商视图中有 40 个项目,但选项卡 1 显示 39 个项目。 And it doesn't show all of floating action button.它没有显示所有的浮动操作按钮。
I think the reason of the problem is Coordinator Layout in fragment_tab1.xml.我认为问题的原因是 fragment_tab1.xml 中的 Coordinator Layout。 Because when i remove it, toolbar disappears and appears when scroll.因为当我删除它时,工具栏消失并在滚动时出现。 But floating action button doesn't disappear and appear when scroll.但是浮动操作按钮在滚动时不会消失并出现。
Here is video : http://sendvid.com/hyaorcss这是视频: http : //sendvid.com/hyaorcss
activity_main.xml:活动_main.xml:
<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:fitsSystemWindows="true"
tools:context="com.example.fab.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabIndicatorColor="@android:color/white"
app:tabSelectedTextColor="@android:color/white"
app:tabGravity="fill"
app:layout_collapseMode="pin" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml: content_main.xml:
<RelativeLayout 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:paddingLeft="6dp"
android:paddingRight="6dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.fab.MainActivity"
tools:showIn="@layout/activity_main">
<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"/>
</RelativeLayout>
fragment_tab1.xml: fragment_tab1.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/tab1SwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/tab1RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:clickable="true" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/tab1RecyclerView"
app:layout_behavior="com.example.fab.ScrollAwareFABBehavior"
app:layout_anchorGravity="bottom|right|end"
android:id="@+id/emailFab"/>
</android.support.design.widget.CoordinatorLayout>
fragment_tab2.xml: fragment_tab2.xml:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tab2SwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab2RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:clickable="true" />
</android.support.v4.widget.SwipeRefreshLayout>
For the collapsing toolbar, you seem to be missing a collapsable toolbar layout.对于折叠工具栏,您似乎缺少可折叠的工具栏布局。 It looks something like this:它看起来像这样:
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
...
You should check out this really good app example demonstrating material design.你应该看看这个展示材料设计的非常好的应用程序示例。 It accomplishes a lot of what you are trying to do.它完成了您正在尝试做的很多事情。
Solved.解决了。
I used FloatingActionButton in activity_main.xml, not in fragment_tab1.xml.我在activity_main.xml 中使用了FloatingActionButton,而不是在fragment_tab1.xml 中。
I made ViewPager static in main activity and i used it in my ScrollAwareFABBehavior class.我在主要活动中将 ViewPager 设为静态,并在我的 ScrollAwareFABBehavior 类中使用它。
activity_main.xml:活动_main.xml:
<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:fitsSystemWindows="true"
tools:context="com.example.fab.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabIndicatorColor="@android:color/white"
app:tabSelectedTextColor="@android:color/white"
app:tabGravity="fill"
app:layout_collapseMode="pin" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/tab1RecyclerView"
app:layout_behavior="com.example.fab.ScrollAwareFABBehavior"
app:layout_anchorGravity="bottom|right|end"
android:id="@+id/emailFab"/>
</android.support.design.widget.CoordinatorLayout>
content_main.xml: content_main.xml:
<android.support.v4.view.ViewPager
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/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_group_timeline"/>
fragment_tab1.xml: fragment_tab1.xml:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/tab1SwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:clickable="true" />
fragment_tab2.xml: fragment_tab2.xml:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/tab2SwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:clickable="true" />
ScrollAwareFABBehavior.java: ScrollAwareFABBehavior.java:
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
nestedScrollAxes);
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
dyUnconsumed);
if(MainActivity.viewPager.getCurrentItem() == 0){
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
child.show();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.