【问题标题】:Android Makes Floating Action Menu in FIXED positionAndroid 将浮动操作菜单置于 FIXED 位置
【发布时间】:2017-04-07 05:16:38
【问题描述】:

您好,我是 Android 开发新手。

我设置了以下代码:

listView.setNestedScrollingEnabled(true);

当我滚动 listView 时隐藏顶部工具栏。但是,所有子视图都会被移动,包括浮动操作菜单。

我希望浮动操作菜单保持空闲,固定在该位置,并且不受 NestedScrollingEnabled 行为的影响。显然它会影响父视图。

有什么解决办法吗?谢谢。

我的xml大致是这样的(忽略附件的图片,和xml代码有点不同,因为太长了,但是大意是有的):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:fab="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/fragment_price_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e3e3e3">

    <android.support.v4.widget.SwipeRefreshLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/swipeContainer"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_below="@+id/buttons_selection_container">

           <FrameLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent">

               <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="15dp"
                android:indeterminateTint="@color/colorProgressBarPrimary"
                android:indeterminateTintMode="src_atop"/>

                <TextView
                android:id="@+id/textView_no_data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No Price Data"
                android:layout_gravity="center_horizontal|center_vertical"
                android:textStyle="bold"
                android:textSize="18dp"
                android:visibility="gone"/>

                <ListView
                android:id="@+id/listView_prices"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="true">
                </ListView>
           </FrameLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/price_fragment_float_Menu_button"
        android:layout_width="322dp"
        android:layout_height="376dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="46dp"
        android:layout_marginRight="10dp"
        fab:fab_addButtonColorNormal="@color/color_float_blue_dark"
        fab:fab_addButtonColorPressed="@color/colorAccent"
        fab:fab_addButtonPlusIconColor="@color/white">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/button_switch_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/color_float_blue_lite"
            fab:fab_colorPressed="@color/colorAccent"
            fab:fab_icon="@drawable/button_different_currencies"
            fab:fab_size="mini"
            fab:fab_title="Switch Price"/>
    </com.getbase.floatingactionbutton.FloatingActionsMenu>
</RelativeLayout>

【问题讨论】:

  • 在另一个视图中创建浮动按钮并包含您的布局
  • 使用Google FAB button 而不是自定义的。

标签: android listview scroll fixed floating-action-button


【解决方案1】:

您可以使用自定义布局行为属性在滚动列表视图时隐藏 FAB 按钮

public class ScrollingFABBehavior extends FloatingActionButton.Behavior {


private static final String TAG = "ScrollingFABBehavior";

public ScrollingFABBehavior(Context context, AttributeSet attrs) {
    super();
    // Log.e(TAG, "ScrollAwareFABBehavior");
}


public boolean onStartNestedScroll(CoordinatorLayout parent, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {

    return true;
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    if (dependency instanceof RecyclerView)
        return true;

    return false;
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout,
                           FloatingActionButton child, View target, int dxConsumed,
                           int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    // TODO Auto-generated method stub
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed);
    //Log.e(TAG, "onNestedScroll called");
    if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
     //   Log.e(TAG, "child.hide()");
        child.hide();
    } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
      //  Log.e(TAG, "child.show()");
        child.show();
    }
}}

布局xml

<android.support.design.widget.FloatingActionButton
    android:id="@+id/imageViewYes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end|right"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_yes"
    app:backgroundTint="@color/white"
    android:scaleType="center"
    app:elevation="6dp"
    app:fabSize="normal"
    app:layout_behavior="com.your.package.ScrollingFABBehavior" />

【讨论】:

  • 不好。我希望用户始终看到浮动按钮。当列表视图到达结果的最后一行时,它只会淡入 0.4f alpha。我现在的解决方案是目前还没有。让它向上和向下取决于滚动行为。将尝试将浮动按钮放在列表视图布局中。
猜你喜欢
  • 2016-07-26
  • 2016-10-10
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 2015-12-31
相关资源
最近更新 更多