简体   繁体   English

滚动到回收站视图中下一个项目的顶部

[英]Scroll to top of next item in a recyclerview

I have the following code which detects that whether a swipe in onTouch is Left / Right. 我有以下代码,用于检测onTouch中的滑动是否为向左/向右。 But i want to detect top/down. 但是我想检测顶部/底部。 Please suggest changes in the code as to how i can achieve that by modifying the code 请建议修改代码,以了解如何通过修改代码来实现

r.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int downX = 0,upX;
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:{
                    downX = (int) event.getX();}
                case MotionEvent.ACTION_UP:{
                    upX = (int) event.getX();

                    float deltaX = downX - upX;
                    Log.e("DeltaX","Dd "+deltaX);
                     if(Math.abs(deltaX)>0){
                        if(deltaX>=0){

                            return true;
                        }else{

                            return  true;
                        }
                    }
                    else {

                    }
                }
            }

            return false;
        }
    });

Please dont suggest any 3 rd party libraries or any other solution. 请不要建议任何第三方库或任何其他解决方案。 Please suggest changes in the above code only. 请仅建议对以上代码进行更改。 I have tried a lot of other things, but this is a perfect fit 我已经尝试了很多其他方法,但这非常合适

Thank you for responding! 感谢您的回应!

here is the code i figured out 这是我想出的代码

  r.addOnScrollListener(new RecyclerView.OnScrollListener() {
        public boolean top;

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


            if (top) {
                int index = mLay.findFirstVisibleItemPosition();
                r.smoothScrollToPosition(index);

            } else {
                int index = mLay.findLastVisibleItemPosition();
                r.smoothScrollToPosition(index);


            }


        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (dy > 0) {
                top = false;
            } else {

                top = true;
            }

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM