Three listviews together with one scroll listener are not clickable

183 views Asked by At

I have three listviews. They scroll together (if I scroll one, then the other scrolls too). But I want each list be clickable separately, because if I click now it get firstly operation one then operation two if I click one more time. So if I scroll one list, then all three of them are scrolled (this is implemented in the code below), but if I click on one of them, I want to do one operation, if I click on other list, then there should be done other operation (and if on the third, then third).

When I tried just to add item listener for each list then it starts the operation #1, #2, #3 consecutively no matter which listview is clicked.

I tried to use getId, but it returns the same id.

Don't know what to do.

public void setUpListeners(final ListView listView, final ListView listView2, final ListView listView3)    {

    final View[] clickSource = new View[1];
    final View[] touchSource = new View[1];
    final int offset = 0;        

    listView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (touchSource[0] == null)
                touchSource[0] = v;

            if (v == touchSource[0]) {
                listView.dispatchTouchEvent(event);
                listView3.dispatchTouchEvent(event);
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    clickSource[0] = v;
                    touchSource[0] = null;
                }
            }

            return false;
        }
    });

    listView3.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (touchSource[0] == null)
                touchSource[0] = v;

            if (v == touchSource[0]) {
                listView.dispatchTouchEvent(event);
                listView2.dispatchTouchEvent(event);
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    clickSource[0] = v;
                    touchSource[0] = null;
                }
            }

            return false;
        }
    });

    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if(view == clickSource[0])
                listView2.setSelectionFromTop(firstVisibleItem, view.getChildAt(0).getTop() + offset);
        }
    });
}
1

There are 1 answers

0
Max K. On

Solved it myself (so this is triple, or double if you remove one, list that scrolls synchronously but each list has its own clicklistener):

final View[] clickSource = new View[1];
    final View[] touchSource = new View[1];

    listView.setOnTouchListener(new View.OnTouchListener() {
        float touched;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (touchSource[0] == null)
                touchSource[0] = v;

            if (v == touchSource[0]) {
                listView2.dispatchTouchEvent(event);
                listView3.dispatchTouchEvent(event);

                if (event.getAction() == MotionEvent.ACTION_UP) {

                    if(Math.abs( touched - event.getY() ) < 10) {
                        int position = listView.pointToPosition((int) event.getX(), (int) event.getY());
                       ///operation #1
                    }
                    touchSource[0] = null;
                }
                else if(event.getAction() == MotionEvent.ACTION_DOWN)   {

                    touched = event.getY();

                }
            }

            return false;
        }
    });

    listView2.setOnTouchListener(new View.OnTouchListener() {
        float touched;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (touchSource[0] == null)
                touchSource[0] = v;

            if (v == touchSource[0]) {
                listView.dispatchTouchEvent(event);
                listView3.dispatchTouchEvent(event);
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    AbsListView alv = (AbsListView) v;
                    if(Math.abs( touched - event.getY() ) < 10) {

                        int position = listView.pointToPosition((int) event.getX(), (int) event.getY());
                        clickOnLocation(position);
                       ///operation #2
                    }
                    touchSource[0] = null;
                }
                else if(event.getAction() == MotionEvent.ACTION_DOWN)   {

                    touched = event.getY();

                }
            }
            return false;
        }
    });

    listView3.setOnTouchListener(new View.OnTouchListener() {
        float touched;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (touchSource[0] == null)
                touchSource[0] = v;

            if (v == touchSource[0]) {
                listView.dispatchTouchEvent(event);
                listView2.dispatchTouchEvent(event);
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    if(Math.abs( touched - event.getY() ) < 10) {

                        int position = listView.pointToPosition((int) event.getX(), (int) event.getY());
                        clickOnLocation(position);
                       ///operation #3
                    }
                    touchSource[0] = null;
                }
                else if(event.getAction() == MotionEvent.ACTION_DOWN)   {

                    touched = event.getY();

                }
            }
            return false;
        }


    });