android - 自定义 CoordinatorLayout 行为

标签 android android-coordinatorlayout

这是我的布局:

<android.support.design.widget.CoordinatorLayout
   android:id="@+id/coordinator_layout"
   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">

<include layout="@layout/view_product_details"/>  // This contains a NestedScrollView

<LinearLayout android:id="@+id/indicator"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="right|bottom"
              android:layout_marginBottom="16dp"
              android:gravity="right|bottom"
              android:orientation="horizontal"
              app:layout_behavior="utils.CustomBehavior">

            // Some views.. 
</LinearLayout>

我希望当用户打开 Activity 时显示指示器布局,但当用户向下滚动时它应该隐藏。
当用户滚动回页面顶部时,我希望它再次显示。

所以我写了一个自定义行为类:

public class CustomBehavior extends CoordinatorLayout.Behavior {

private int totalY;

public CustomBehavior(Context context, AttributeSet attrs) {
    super();
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
    return dependency instanceof NestedScrollView;
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View 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, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

    totalY += dyConsumed;

    Log.d("Scroll", "Total Y : " + totalY);

    LinearLayout fabMenu = (LinearLayout) child;
    FloatingActionButton wishListButton = (FloatingActionButton) fabMenu.findViewById(R.id.fab_wishlist);
    FloatingActionButton collectionButton = (FloatingActionButton) fabMenu.findViewById(R.id.fab_collection);
    FloatingActionButton tastedButton = (FloatingActionButton) fabMenu.findViewById(R.id.fab_tasted_list);

    if (totalY > 10) {
        wishListButton.hide(true);
        collectionButton.hide(true);
        tastedButton.hide(true);
    } else if (totalY < 10) {
        wishListButton.show(true);
        collectionButton.show(true);
        tastedButton.show(true);
    }
}
}

但是总Y值的加减不靠谱。
如何检测到用户已到达 NestedScrollView 的顶部?

或者有更好的方法来实现这样的事情吗? 谢谢!

最佳答案

查看以下内容是否有帮助 - 在 onNestedScroll() 中:

使用

 if (target instanceof NestedScrollView) {
        final NestedScrollView nestedScrollView = (NestedScrollView) target;
        totalY = nestedScrollView.getScrollY();     
 }

代替

totalY += dyConsumed;

关于android - 自定义 CoordinatorLayout 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32692372/

相关文章:

java - 错误: cannot resolve symbol 'edit_message' in MyFirstApp tutorial

java - Android 时间选择器弹出问题

Android - 将 Jsoup 与 android_asset html 文件一起使用

java - Apache POI : OutOfMemory

android - CoordinatorLayout (CL) : NSV Not at Top When Loaded 中的 NestedScrollView (NSV)

android - 我可以将嵌套的 FloatingActionButton 锚定到视口(viewport)吗?

android - 将页脚添加到 CoordinatorLayout

android - 内容提供者 Realm

android - float 操作按钮未显示 - viewpager

android - 扩展持久性 Bottom Sheet 时调整回收器 View 高度