当任何一个子视图被重绘时,如何触发父视图的重绘? [英] How do I trigger the redraw of a parent View when any of it's children are redrawn?

查看:361
本文介绍了当任何一个子视图被重绘时,如何触发父视图的重绘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经基于LinearLayout编写了一个自定义的Android View,我将其称为ReflectingLayout.这个想法很简单,可以在ReflectingLayout中声明的所有子View之下渲染反射效果.例如...

I have written a custom Android View based on a LinearLayout which I have called ReflectingLayout. The idea is fairly simple, to render a reflection effect below any child Views declared within the ReflectingLayout. e.g...

我正在通过覆盖ReflectingLayout中的dispatchDraw()来实现此目标,如下所示:

I am achieving this by overriding dispatchDraw() in ReflectingLayout as follows:

@Override
protected void dispatchDraw(Canvas canvas) {

  Bitmap bitmap = Bitmap.createBitmap(...); 
  Canvas tempCanvas = new Canvas(bitmap);

  // Draw child views to bitmap using tempCanvas
  super.dispatchDraw(tempCanvas);

  ...
  // some tranformations performed on bitmap
  ...

  // draw transformed image on to main canvas
  canvas.drawBitmap(bitmap, 0, 0, null);
}

问题

如果我的ReflectingLayout包含一个TextView,并且我更改了该TextView的文本内容,则反射效果不会更新.

The Problem

If my ReflectingLayout contains, say, a TextView and I change the text content of that TextView the reflection effect is not updating.

我相信这是因为Android只会重绘TextView本身而不是所有父视图,这意味着不会调用我覆盖的dispatchDraw()方法.

I believe this is because Android will only redraw the TextView itself rather than all the parent views as well, meaning my overridden dispatchDraw() method does not get called.

是否有任何简单的方法可以触发父视图的重绘,以响应其子视图中任何一个的更改?

特别是,重绘子视图时如何重绘ReflectingLayout?

In particular, how can I make my ReflectingLayout be redrawn when child views are redrawn?

  • 我注意到一个新的 View.OnLayoutChangeListener 是在API级别11中添加的.大概可以将其用于注册到父视图的回调并触发redraw(?).无论如何,我需要一个可以在API Level 7以上运行的解决方案.

  • I've noticed that a new View.OnLayoutChangeListener was added in API Level 11. Presumably this could be used to register a callback to the parent view and trigger a redraw(?). In any case, I need a solution which will work on API Level 7 upwards.

我可以简单地扩展TextView和我想在ReflectingLayout中声明的任何其他View类,以在重绘父级时使它们的父级无效,但这是一个笨拙的解决方案.

I could simply extend TextView and any other View class I want to declare inside ReflectingLayout to invalidate their parent as they are redrawn, but this is a clunky solution.

任何得到极大帮助的人(包括如果您认为总体方法错误的人).

Any help greatly received (including if you think the overall approach is wrong).

谢谢.

推荐答案

在您的视图中使用getParent()方法怎么样?像这样:

What about using getParent() method in your view? Something like this:

if (getParent() instanceof View)
    ((View) getParent()).invalidate();

这篇关于当任何一个子视图被重绘时,如何触发父视图的重绘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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