自定义列表视图绘图 [英] Custom listview drawing

查看:124
本文介绍了自定义列表视图绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上面画我的ListView覆盖。于是我就用最直接的方法,并把我的自定义绘制code下super.onDraw(画布)在我的ListView子类。

让我惊讶的名单是我上面的自定义绘图渲染。我知道这很容易被包裹在ListView中的FrameLayout做,但我不认为这是neccesary夸大另一种观点。修复程序以及为什么我就遇到了这个问题的解释是真棒,谢谢!

我的平局code看起来是这样的:

  @覆盖
保护无效的OnDraw(帆布油画){
    super.onDraw(画布);
    canvas.drawRect(矩形,油漆);
}
 

解决方案

有关ListView控件的事情是,它不是实际绘制列表内容。这是通过绑定到列表中的适配器处理 - 或更具体地说是填充ListView由适配器创建的视图

在ListView的OnDraw中发生的载视图中创建并显示之前。

在一个ListView的顶部绘制的东西可以是昂贵的,因为你是合成的另一层上面本来就复杂的视图。放置在顶部的另一个视图(使用的FrameLayout你的建议)可能是最好的方式做你正在尝试什么,或者(取决于你想要达到的效果),你可以修改适配器或意见显示你想覆盖的数据。

I am trying to draw an overlay above my ListView. So i went with the most obvious approach and put my custom drawing code beneath super.onDraw(canvas) in my ListView subclass.

To my surprise the list was rendered above my custom drawing. I know this can easily be done by wrapping the ListView in a FrameLayout but i don't think it is neccesary to inflate another view. A fix and an explanation of why i ran into this problem would be awesome, thanks!

My draw code looks something like this:

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

解决方案

The thing about the ListView is that it isn't actually drawing the list contents. That's handled by the Adapter bound to the list -- or more specifically the Views created by that Adapter that populate the ListView.

The onDraw of the ListView happens before the contained Views are created and displayed.

Drawing things on top of a ListView can be expensive, as you're compositing another layer above an already complex View. Placing another View on top (using a FrameLayout as you suggest) is probably the "best" way to do what you're attempting, or alternatively (depending on the effect you're trying to achieve), you could modify the Adapter or Views to display the data you're trying to overlay.

这篇关于自定义列表视图绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆