自动滚动时使用RecycleView TouchListener

问题描述 投票:1回答:1

我尝试使用AutoScroll功能制作RecyclerView。现在工作正常。但是我想用AutoScroll来TouchEvent。这意味着如果用户未触摸,列表将自动滚动。如果用户触摸,列表就会跟随用户的手指移动。但是现在,如果用户触摸列表,列表就停止了,但是1秒钟后,即使仍然触摸手指,它也会再次移动。

这是我的代码。

recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {

            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                switch (e.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        isTouched = true;
                        return false;
                    case MotionEvent.ACTION_MOVE:
                        isTouched = true;
                        return false;
                    case MotionEvent.ACTION_UP:
                        isTouched = false;
                        return true;
                }
                return super.onInterceptTouchEvent(rv, e);
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(final RecyclerView view, int scrollState) {
                Log.e("test", isTouched+"");
                if (!isTouched) {
                    //list is top
                    if (!recyclerView.canScrollVertically(-1)) {
                        listHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                view.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
                                GoingDown = true;
                            }
                        }, 1000);// 1 second delay
                        //list is bottom
                    } else if (!recyclerView.canScrollVertically(1)) {
                        listHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                view.smoothScrollToPosition(0);
                                GoingDown = false;
                            }
                        }, 1000);// 1 second delay
                        //list is middle
                    } else {
                        if (GoingDown) {
                            listHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    view.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
                                    GoingDown = true;
                                }
                            }, 500);// 1 second delay
                        } else {
                            listHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    view.smoothScrollToPosition(0);
                                    GoingDown = false;
                                }
                            }, 500);// 1 second delay
                        }
                    }
                }
            }
        });

为了正常工作,我应该解决什么?

p.s]对不起,我的英文简短。

我尝试使用AutoScroll功能制作RecyclerView。现在工作正常。但是我想用AutoScroll来TouchEvent。这意味着如果用户未触摸,列表将自动滚动。如果用户触摸,列表移动...

android android-recyclerview ontouchlistener autoscroll onscrolllistener
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.