2016-11-23 124 views
0

您好我是Android開發新手。Android使固定位置的浮動操作菜單

我把下面的代碼:

listView.setNestedScrollingEnabled(true); 

隱藏頂部的工具欄,當我滾動列表視圖。但是,所有子視圖都會移動,包括「浮動操作菜單」。

我希望浮動操作菜單保持空閒,固定在該位置,並且不受NestedScrollingEnabled行爲的影響。顯然它會影響父視圖。

enter image description here

任何解決這個?謝謝。

我的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> 
+0

在另一個視圖中創建浮動按鈕幷包含您的佈局 – Vadivel

+0

使用[Google FAB按鈕](https://guides.codepath.com/android/floating-action-buttons)而不是自定義按鈕。 –

回答

0

您可以隱藏當列表視圖中使用自定義佈局行爲滾動atrribute

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 FAB按鈕

<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

不好。我希望用戶總是看到浮動按鈕。當列表視圖到達結果的最後一行時,它只會褪色到0.4f alpha。我現在的解決方案目前還沒有。保持它的上下取決於滾動行爲。將嘗試將浮動按鈕放入列表視圖佈局中。 – felixwcf