Gridview Scroll с помощью OnTouchListener - PullRequest
0 голосов
/ 02 сентября 2018

В основном, я хочу быстро выбрать элемент, а также прокрутить Gridview. В OnItemClickListener прокрутка работает нормально, но не быстро выбрать несколько элементов, таких как OnTouchListener.

Мой код:

gridView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {

            int action = me.getActionMasked();  
            float currentXPosition = me.getX();
            float currentYPosition = me.getY();
            int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);

            switch (action) {
                case (MotionEvent.ACTION_MOVE):

                break;
                case (MotionEvent.ACTION_UP):
                    if (position != -1) {
                        //Here my logic to add item in basket list 
                        return true;
                    }
            }         
            return false;
        }
    });

Прямо сейчас его прокрутка и добавление элемента. Как мне остановить ACTION_UP(selection Item) событие после ACTION_MOVE(when scroll)?

1 Ответ

0 голосов
/ 30 сентября 2018

Я применял этот код для просмотра сетки прокрутки на OntouchListener и запретил прокрутку на выбранном элементе. И отлично работают на моей стороне. Если у кого-то есть лучшее решение, принесите его.

Мой код:

private int moveCount=0;
private boolean ignore = false;

gridView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {

            v.getParent().requestDisallowInterceptTouchEvent(true);
            int action = me.getActionMasked();  // MotionEvent types such as ACTION_UP, ACTION_DOWN
            float currentXPosition = me.getX();
            float currentYPosition = me.getY();
            int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
            if (ignore && action == MotionEvent.ACTION_UP)
                return false;
            switch (action) {
                case (MotionEvent.ACTION_MOVE):
                    moveCount++;
                    Log.d(DEBUG_TAG, "Action was MOVE " + position);
                    if(moveCount>3) {
                        ignore = true;
                    }

                    break;
                case (MotionEvent.ACTION_UP):
                    addItem(position);
                    Log.d(DEBUG_TAG, "Action was UP " + position);
                    return true;

                case (MotionEvent.ACTION_DOWN):
                    Log.d(DEBUG_TAG, "Action was DOWN " + position);
                    moveCount=0;
                    ignore = false;
                    return true;
                case (MotionEvent.ACTION_CANCEL):
                    addItem(position);
                    moveCount=0;
                    ignore = false;
                    gridView.setFocusable(true);

                    Log.d(DEBUG_TAG, "Action was CANCEL " + position);
                    return true;
                case (MotionEvent.ACTION_OUTSIDE):
                    Log.d(DEBUG_TAG, "Movement occurred outside bounds " +
                            "of current screen element " + position);
                    return true;

            }
            Log.d("clickTouch=", "" + position);
            return false;
        }
    });
...