D
D
dev0dev2018-07-12 13:04:19
Android
dev0dev, 2018-07-12 13:04:19

Click view is not working properly, how to fix it?

Hello

<ScrollView>
  <HorizontalScrollView>
    <TableLayout>
    </TableLayout> 
  </HorizontalScrollView>
</ScrollView>

There is this code
MainActivity
for (int i = 0; i < 10; i++) {
            TableRow tableRow = new TableRow(this);
            for (int j = 0; j < 10; j++) {
               ImageView img = new ImageView(this);
               ....
               tableRow.addView(img);
            }
            tableLayout.addView(tableRow);
}

I also override the method in the Activity
@Override
    public boolean onTouchEvent(MotionEvent event) {
        float curX, curY;
            switch (event.getAction()) {
                case ACTION_DOWN:
                    mx = event.getX();
                    my = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    curX = event.getX();
                    curY = event.getY();
                    vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    mx = curX;
                    my = curY;
                    break;
                case MotionEvent.ACTION_UP:
                    curX = event.getX();
                    curY = event.getY();
                    vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    break;
            }
        return true;
    }

So I implemented vertical and horizontal scrolling and everything works well if the elements do not fit on the screen, then scrolls appear and I can scroll up and down and in any direction.
But here a problem arises, if I hang a click event on the ImageView,
gifImageView.setOnClickListener
then when scrolling, it drops to different places, that is, there is no smooth scrolling without a click event (if it is not set)
, as I understand it, the problem is in the OnTouchListener of the ImageView itself, if you create your own view from ImageView and override method like this onTouchEvent
@Override
        public boolean onTouchEvent(MotionEvent ev) {
            return false;
        }

then the scroll works as it should, but the click does not work, how can I solve it? I get in the tableLayout I add 100 dynamic ImageViews and when I set a click event, I feel that the system freezes or something, without a click everything works smoothly, but a click is needed to understand which picture was clicked,
how can this be solved at all?
Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question