【问题标题】:Can I control scrollview speed on while scrolling?我可以在滚动时控制滚动视图的速度吗?
【发布时间】:2017-03-04 12:01:22
【问题描述】:

我在布局中使用 ScrollView,我需要控制在 scrollview 中的滚动速度。已经使用了很多东西。比如对象动画器、计时器,但这些对我没有帮助。那么我该如何控制(或)设置在 android 上的滚动视图中滚动速度有限。

【问题讨论】:

  • 这里已经有人问过这个问题,比如Reduce speed of smooth scroll in scroll view,你也问同样的问题吗?
  • 但这不是我的问题的正确解决方案。上面的链接答案工作“自动滚动”不能控制滚动的速度。我已经尝试过这个。它不能控制速度......当用户滚动...

标签: android android-layout android-scrollview


【解决方案1】:

这适用于ViewPager,从未尝试过ScrollView

public class FixedSpeedScroller extends OverScroller {

    private int mDuration = 2000;

    public FixedSpeedScroller(Context context) {
        super(context);
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }


    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    public void setFixedDuration(int duration) {
        this.mDuration = duration;
    }
}

如何使用 -

try {
   Field _mScroller = ScrollView.class.getDeclaredField("mScroller");
   _mScroller.setAccessible(true);
   FixedSpeedScroller scroller = new FixedSpeedScroller(mPager.getContext(), new AccelerateDecelerateInterpolator());
   scroller.setFixedDuration(600);
   _mScroller.set(mScrollView, scroller);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
    Log.e("TAG", "Scroller exception"  +e.getLocalizedMessage());
}

试一试。

【讨论】:

  • 不是一个好习惯。只需使用上面的代码,让我知道会发生什么,是触发异常还是什么?
【解决方案2】:

如何控制 Android ListView 的滚动速度

您可以在 Fragment 中使用如下代码控制 Android ListView 的滚动速度:

@Override
public void onStart() {
    super.onStart();
    // scroll speed decreases as friction increases. a value of 2 worked
    // well in an emulator; you need to test it on a real device
    getListView().setFriction(ViewConfiguration.getScrollFriction() * 2);
}

默认ListView默认滚动速度太快,导致滚动很多项目时眼睛很难看

【讨论】:

    猜你喜欢
    • 2011-09-13
    • 2021-04-25
    • 2015-02-08
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多