簡體   English   中英

活動中的浮動操作按鈕 - 如何錨定到片段中的recyclerview?

[英]Floating Action Button in an Activity - how to anchor to a recyclerview in a fragment?

我在這個問題中遇到了同樣的問題浮動操作按鈕沒有完全顯示在片段內

我的activity_main中有一個tablayout和兩個標簽(包含不同的片段,其中一個包含recyclerView)。 當我將FAB放入片段時,在我向下滾動recyclerView之前,FAB沒有完全顯示。

因此,我按照鏈接問題中的建議將我的FAB移動到coordinator_layout小部件中的activity_main.xml文件中,並且它運行良好。

通過這種方式,我在活動中有一個FAB,而不是在片段中,我想知道如何將我的工廠錨定到片段中的recyclerview,例如讓它在回收器卷軸中設置動畫?

現在帶有tablayout的activity_main.xml:

activity_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"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar1"
            android:background="?attr/colorPrimary"
            android:scrollbars="horizontal"
            app:tabMode="scrollable" />

    </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.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_action_location_found"
        app:fabSize="normal" />
    </android.support.design.widget.CoordinatorLayout>

fragment.xml之

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_vertical_margin" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/start_button"
        android:scrollbars="vertical" />


</RelativeLayout>

你試過將它錨定到viewpager嗎?

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    app:layout_anchor="@id/viewpager"
    app:layout_anchorGravity="bottom|right|end"
</android.support.design.widget.CoordinatorLayout>

然后擴展一個FloatingActionButton.Behavior以響應CoordinatorLayout事件:

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 (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            child.hide();
         } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
             child.show();
         }
     }

 }

並將其添加到您的FloatingButton:

app:layout_behavior="com.package.yourapp.ScrollAwareFABBehavior"

這里有很好的解釋:

https://guides.codepath.com/android/Floating-Action-Buttons

補充@ petrusgome的答案:

提供的解決方案對我不起作用。 我有一個問題,在滾動Recyclerview列表后,FAB不再顯示。 要修復它,我只需child.hide()以下代碼替換child.hide()

child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
    @Override
    public void onHidden(FloatingActionButton fab) {
        super.onHidden(fab);
        fab.setVisibility(View.INVISIBLE);
    }
});

完整的解釋可以在這里找到: 滾動問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM