Android 材料组件中新的 ExtendedFloatingActionButton 的收缩和扩展功能不起作用

use*_*825 6 android material-design floating-action-button androidx

我正在使用ExtendedFloatingActionButtonMaterial Components for Android 库中的新内容1.1.0-alpha06。它呈现得很好,但 'extend' 和 'shrink' 方法没有做任何事情。

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
                    android:id="@+id/extended_fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_anchor="@id/bottom_sheet"
                    android:text="Drag map to change location"
                    app:icon="@drawable/my_location"
                    app:backgroundTint="@color/white"
                    app:iconTint="@color/quantum_googblueA200"
                    android:textColor="@color/quantum_googblueA200"
                    app:iconSize="18dp"
                    style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
                    android:padding="4dp"
                    android:textSize="12sp"
                    android:textAllCaps="false"
                    android:layout_margin="8dp"
                    app:layout_anchorGravity="right|top"/>
Run Code Online (Sandbox Code Playgroud)

这是渲染的布局:

渲染布局

Chr*_*ute 5

这是一个 Kotlin 版本,它与内置的通讯录应用程序的行为相匹配。只要RecyclerView位于顶部,FAB 就会扩展,而每当用户从顶部滚动时 FAB 就会收缩。

class FabExtendingOnScrollListener(
    private val floatingActionButton: ExtendedFloatingActionButton
) : RecyclerView.OnScrollListener() {
    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        if (newState == RecyclerView.SCROLL_STATE_IDLE
            && !floatingActionButton.isExtended
            && recyclerView.computeVerticalScrollOffset() == 0
        ) {
            floatingActionButton.extend()
        }
        super.onScrollStateChanged(recyclerView, newState)
    }

    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        if (dy != 0 && floatingActionButton.isExtended) {
            floatingActionButton.shrink()
        }
        super.onScrolled(recyclerView, dx, dy)
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

recyclerView.addOnScrollListener(FabExtendingOnScrollListener(fab))
Run Code Online (Sandbox Code Playgroud)


Amj*_*reh 4

我已经在我的代码中做到了这一点。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                extendedFloatingActionButton.extend();
            }
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0 || dy < 0 && extendedFloatingActionButton.isExtended()) {
                extendedFloatingActionButton.shrink();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)