Custom drawing list

I am trying to draw a caption above a ListView. So I went with the most obvious approach and put my own drawing code under super.onDraw (canvas) in my ListView subclass.

To my surprise, the list was provided above my custom drawing. I know this can be done easily by wrapping the ListView in a FrameLayout, but I don't think the other view needs to be inflated. Fixing and explaining why I am facing this issue would be awesome, thanks!

My drawing code looks something like this:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRect(rect, paint);
}

      

+3


source to share


2 answers


The thing about the ListView is that it doesn't actually draw the contents of the list. This is handled by the adapter associated with that list, or more specifically, the views created by that adapter that populate the ListView.

The OnDraw ListView occurs before the contained views are created and displayed.



Drawing things on top of the ListView can be costly as you are compositing another layer over an already complex view. Placing another top view (using a FrameLayout as you suggest) is probably the "best" way to do what you are trying, or alternatively (depending on the effect you are trying to achieve) you can change the adapter or views to displaying the data you are trying to overlay.

+6


source


Switching dispatchDraw

instead is onDraw

similar to what you want:



@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    ...
}

      

+4


source







All Articles