android 滚动条不消失,Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结...

先来段Behavior代码,网上关于FloatingActionButton(以下简称FAB)滑动的代码很多了,参考一下。

public class FabBehavior extends FloatingActionButton.Behavior{

private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();

private boolean mIsAnimatingOut = false;

public FabBehavior(Context context, AttributeSet attrs) {

super();

}

@Override

public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,

final View directTargetChild, final View target, final int nestedScrollAxes) {

// Ensure we react to vertical scrolling

return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL

|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);

}

@Override

public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,

final View target, final int dxConsumed, final int dyConsumed,

final int dxUnconsumed, final int dyUnconsumed) {

super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {

// User scrolled down and the FAB is currently visible -> hide the FAB

animateOut(child);

} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {

// User scrolled up and the FAB is currently not visible -> show the FAB

animateIn(child);

}

}

// Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits

private void animateOut(final FloatingActionButton button) {

if (Build.VERSION.SDK_INT >= 14) {

ViewCompat.animate(button).translationY(button.getHeight() + getMarginBottom(button)).setInterpolator(INTERPOLATOR).withLayer()

.setListener(new ViewPropertyAnimatorListener() {

public void onAnimationStart(View view) {

FabBehavior.this.mIsAnimatingOut = true;

}

public void onAnimationCancel(View view) {

FabBehavior.this.mIsAnimatingOut = false;

}

public void onAnimationEnd(View view) {

FabBehavior.this.mIsAnimatingOut = false;

view.setVisibility(View.GONE);

}

}).start();

} else {

}

}

// Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters

private void animateIn(FloatingActionButton button) {

button.setVisibility(View.VISIBLE);

if (Build.VERSION.SDK_INT >= 14) {

ViewCompat.animate(button).translationY(0)

.setInterpolator(INTERPOLATOR).withLayer().setListener(null)

.start();

} else {

}

}

private int getMarginBottom(View v) {

int marginBottom = 0;

final ViewGroup.LayoutParams layoutParams = v.getLayoutParams();

if (layoutParams instanceof ViewGroup.MarginLayoutParams) {

marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;

}

return marginBottom;

}

}

这是自定义的一个Behavior类,主要在onNestedScroll中自定义了出现和消失的动画。使用的时候,在xml文件中给FAB加一个包含完整behavior类名的layout_behavior属性

app:layout_behavior="com.normalframe.widgets.view.FabBehavior"

这样FAB就会随着列表上滑消失,下滑出现。这个功能主要是要处理FAB的位置会使列表最后一项被挡住的问题,当上滑时,FAB隐藏,这样当到达列表底部最后一项时,由于刚刚的动作是上滑动作,所以FAB处于隐藏状态,不会遮挡到列表。

在写这个功能时,发现了一个问题:

上滑时FAB能够正常隐藏,但是下拉列表时,FAB就不出现了。

而一样的代码如果放到其它项目中,有些又可以正常实现功能。Debug的时候发现,上拉时会调用onNestedScroll,于是其中自定义的隐藏方法可以被调用,但下滑时,不调用onNestedScroll。

以上所述是小编给大家介绍的Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个相对复杂的问题,需要一定的Android开发经验和知识。我会简单地讲解一下实现的大致思路和步骤。 1. 自定义LayoutManager 首先需要自定义一个LayoutManager,它会负责RecyclerView中每个item的布局排列和滚动。在这个LayoutManager中,我们需要重载一些方法,例如`onLayoutChildren()`,`scrollHorizontallyBy()`,`generateDefaultLayoutParams()`等。这些方法的具体实现会根据我们的需求而不同。其中,`onLayoutChildren()`方法负责测量和布局RecyclerView中的每一个ItemView,`scrollHorizontallyBy()`方法负责水平滚动RecyclerView,`generateDefaultLayoutParams()`方法负责生成默认的LayoutParams。 2. 实现弧形效果 要实现弧形效果,我们需要使用贝塞尔曲线。具体来说,我们可以通过计算贝塞尔曲线上的点来实现在RecyclerView中弧形排列ItemView。这个过程可以在`onLayoutChildren()`方法中实现。我们可以通过计算每个ItemView的位置和角度来获得它的坐标,然后将这些坐标传递给View进行布局。 3. 实现滑动放大效果 要实现滑动放大效果,我们可以在`scrollHorizontallyBy()`方法中监听RecyclerView的滑动距离,然后根据距离计算每个ItemView的缩放比例。具体来说,我们可以在滑动过程中计算每个ItemView的中心点和RecyclerView的中心点之间的距离,然后根据距离计算缩放比例。我们可以使用`setScaleX()`和`setScaleY()`方法来设置ItemView的缩放比例。 以上是实现弧形以及滑动放大效果RecyclerView的大致思路和步骤。具体的实现过程中还需要考虑很多细节问题,例如ItemView的位置和角度计算、动画效果的处理等等。如果您需要更详细的信息,请参考相关的开发文档或者查找相关的示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值