BottomSheet滑动得很慢 [英] BottomSheet sliding very slowly

查看:113
本文介绍了BottomSheet滑动得很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我想根据bottomSheet的slideOffset更改视图的填充.但是,当我尝试在BoottomSheetBehaviour回调上更改视图的填充时,BottomSheet的滑动速度会变慢.这是我的代码:

Hello,
i want to change a view's padding according to bottomSheet's slideOffset. But when i tried to change view's padding on BoottomSheetBehaviour Callback, BottomSheet sliding speed goes slow down. here is my code:

View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
            bottomSheetExpended = false;
        } else if (newState == BottomSheetBehavior.STATE_EXPANDED) {
            bottomSheetExpended = true;
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        int padding = (int) (10 * slideOffset);
        nestedScrollView.setPadding(padding, 0, padding, 0);
    }
});

尝试更改nestedScrollview的填充.
如何解决这个问题?

Trying to change nestedScrollview's Padding.
How to solve this problem?

推荐答案

向上滑动时,slideOffset从0变为1;向下滑动时,由1变为0.如果您想从向上滑动时没有填充到向下滑动时都没有填充,那么就可以这样做.

The slideOffset goes from 0 to 1 as you slide up and from 1 to 0 as you slide down. If you have want to go from having padding to no padding as you slide up and from no padding to having padding as you slide down, then do it like this.

View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet);
// Get Padding value outside of onSlide
final float originalPadding = getActivity().getResources().getDimension(R.dimen.original_padding);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
            bottomSheetExpended = false;
        } else if (newState == BottomSheetBehavior.STATE_EXPANDED) {
            bottomSheetExpended = true;
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        nestedScrollView.setPadding(Math.round(originalPadding * (1 - slideOffset)),
                0, Math.round(originalPadding * (1 - slideOffset)), 0);
    }
});

要以相反的方式进行操作,只需不要从滑动偏移量中减去1.

To do it the opposite way, just don't subtract 1 from the slide offset.

注意:我只使用Math.round()是因为我从onSlide()之外的 dimens资源中获取填充作为浮动对象.

Note: I am only using Math.round() because I am getting the padding as a float from the dimens resources outside of onSlide().

不要在onSlide()内做任何占用大量资源的事情,因为它被称为一堆,这可能是您的底表缓慢滑动的原因,即使它看起来并不像您在这里那样做.

Don't do anything too resource intensive inside of onSlide() because it gets called a bunch and that could be the reason your bottomsheet is sliding slowly even though it doesn't look like you are doing that here.

此外,您无需跟踪底表"的状态,因为您可以随时致电:

Also, you don't need to keep track of the Bottom Sheet's state because you can always call:

if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
  // Bottom sheet is expanded 
}
else if (behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
  // Bottom sheet is collapsed
}

onStateChanged()方法更适用于通过显示或隐藏其他视图等方式对正在更改的状态做出反应.

The onStateChanged() method is more for reacting to the state being changed with things like showing or hiding other views, etc.

这篇关于BottomSheet滑动得很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆