RecycleView TouchListener while autoScroll

亡梦爱人 提交于 2020-08-09 21:24:06

问题


I try to make RecyclerView with AutoScroll function. Now it working fine. But I want to TouchEvent with AutoScroll. It means if user didn't touch, list is auto scrolled. And if user touch, list move to follow user's finger. But now if user touch list, list stoped, but after 1 second it move again even still finger is touched.

Here is my code.

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
                        }
                    }
                }
            }
        });

What should I fix for nomally working?

p.s] Sorry for my short English.

来源:https://stackoverflow.com/questions/53758054/recycleview-touchlistener-while-autoscroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!