How to translate the canvas and still receive touch events in the right places

I am trying to create a custom AbsListView (overriding the same material in ListView, GridView and HeaderGridView) that will move all its drawings and touching events based on an external factor (other things that move around the layout).

Gaskets are not an option here, because then the only way to try to control the position of AbsListView is to use the super-shitty method smoothScrollBy(int distance, int duration), and this gives me a number of different problems that I try to overcome simply by drawing everything together in a different area of ​​the screen.

At that moment, I learned that drawing on another area of ​​the screen is as simple as:

   @Override
   protected void onDraw(Canvas canvas) {
      canvas.translate(0, 400);
      super.onDraw(canvas);
   }

but touch events are not translated, and all touches are 400 pixels up. So I also tried to translate the touch:

   @Override
   public boolean dispatchTouchEvent(MotionEvent ev) {
      ev.offsetLocation(0, -400);
      return super.dispatchTouchEvent(ev);
   }

it works relatively well, to the extent that 1) it is called on the call of the click item and 2) the view changes color based on the theme android:state_pressed="true", but the touch buttons Upor are Cancelnever called, and view does not update the drawing to the default color.

I will still be analyzing, but I'm not sure if a sophisticated user adapter with buttons and a gesture detector if this works or not.

So the question is: How can I translate a drawing and still correctly receive touch events?

+4
source share

Source: https://habr.com/ru/post/1541277/


All Articles