决定是否用户启动或以编程方式触发了滚动?

时间:2016-09-20 10:34:49

标签: android horizontal-scrolling horizontalscrollview

只有当HorizontalScrollView被用户滚动时,才需要在HorizontalScrollView侦听器中执行某些逻辑,而不是我在其他代码部分调用horizontalScrollView.scrollBy(dx, 0);方法。

使用标志 - 程序化调用已启动,已完成 - 无法真正解决问题,因为scrollBy方法调用和触发事件处理程序之间存在小的延迟。但在此时间范围内,即使用户也可以触摸并滚动HorizontalScrollView

您会建议采用什么方法?

horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

        @Override
        public void onScrollChanged() {

            if (horizontalState == 0) {
                horizontalState = 2;
            }

            if (horizontalState == 2) {

                int d = ...;

                if (d != 0) {
                    userSettingRecycleView.scrollBy(d , 0);

                } else {
                    horizontalState = 0;
                }
            }
        }
    });
userSettingRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy){

                if (horizontalState == 1) {

                    super.onScrolled(recyclerView, dx, dy);
                    horizontalScrollView.scrollBy(dx, 0);
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {

                    ItemUserSettingRatingActivity.horizontalState = 1;
                }

                if (newState == RecyclerView.SCROLL_STATE_IDLE || newState == RecyclerView.SCROLL_STATE_SETTLING) {
                    ItemUserSettingRatingActivity.horizontalState = 0;
                }
            }
        });

2 个答案:

答案 0 :(得分:0)

我建议在您的程序scrool事件之间禁用用户触摸范围。也就是说,使用those类来创建自定义HorisontalScrollView并以程序方式启用/禁用“点按”捕获。

答案 1 :(得分:0)

使用SCROLL_STATE_DRAGGING

private val scrollListener = object : RecyclerView.OnScrollListener() {
    override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
        if (newState == SCROLL_STATE_DRAGGING) ViewHelpers.hideKeyboard(activity)
    }
}