如何强制以期code下一行之前立即重绘执行 [英] How to force a view to redraw immediately before the next line of code is executed

查看:187
本文介绍了如何强制以期code下一行之前立即重绘执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个,我想改变被点击其子视图(一个ImageButton的)之一,当背景颜色的LinearLayout。我能够做到这一点,但不是立即 - 的变化不会发生在屏幕上,直到后来(在onResume通话过程中,我认为)。我想了解如何强制布局重绘背景设定之后,code执行时的下一行之前。这是我的OnClickListener的OnClick方法按钮:

I have a LinearLayout that I would like to change the background color of when one of its child views (an ImageButton) is clicked. I am able to do this, but not immediately - the change doesn't occur on the screen until later (I think during an onResume call). I would like to find out how to force the layout to redraw after the background is set, before the next line of code executes. Here is the onClick method of my OnClickListener for the button:

public void onClick(View v) {

  LinearLayout parentLayout = (LinearLayout) v.getParent();
  parentLayout.setBackgroundResource(R.color.my_color);
  SystemClock.sleep(1000); //ms

}

sleep命令是在那里测试重绘无论之前或之后发生。其结果是:后。大多数问题关于这个主题(如这里和<一个 href="http://stackoverflow.com/questions/5991968/how-to-force-an-entire-layout-view-refresh">here)说要在视图中使用无效()我已经使用的命令 parentLayout.invalidate(); parentLayout.postInvalidate(); parentLayout.refreshDrawableState(); 在后台和睡眠线,一切都是徒劳的睡眠命令后重绘仍然发生谁能告诉我如何使它立即发生的呢?

The sleep command is in there to test whether the redraw happens before or after it. The result: after. Most questions on this topic (like here and here) say to use invalidate() on the view. I have used the commands parentLayout.invalidate();, parentLayout.postInvalidate();, and parentLayout.refreshDrawableState(); in between the background and sleep lines, all to no avail. The redraw still happens after the sleep command. Can anyone tell me how to make it happen immediately?

其他可能有用的信息:LinearLayout中绑定到ListView的行,上面的OnClickListener是在扩展SimpleCursorAdapter,而不是活动本身的自定义类。 (这样我可以设置一个监听器为每个在ListView行。)

Other possibly useful information: The LinearLayout is bound to the rows of a ListView, and the OnClickListener above is in a custom class that extends SimpleCursorAdapter, not in the activity itself. (This way I can set a listener for each of the rows in the ListView.)

推荐答案

好了,我终于找到了解决这个问题,这很简单。看来,问题只是,我正在执行中的OnClickLIstener可绘制的变化,当我已经做了在OnTouchListener。如果我设置了OnTouchListener我的按钮,看起来对ACTION_DOWN,然后在其父布局运行setBackgroundColor,更改立即发生。

Okay, I finally found a solution to this issue, and it's simple. It seems that the problem was just that I was executing the drawable changes in an OnClickLIstener, when I should have been doing it in an OnTouchListener. If I set an OnTouchListener for my button that looks for ACTION_DOWN and then runs setBackgroundColor on its parent layout, the change occurs immediately.

这是另类,不太可靠的解决方案来自<一个href="http://stackoverflow.com/questions/6608294/how-to-change-a-view-when-another-view-is-$p$pssed-clicked">this帖子。您可以为家长布置背景XML分配一个选择,然后在OnTouchListener如上所述,pssed(真)上的布局运行一套$ P $。这不是一个很好的解决方案,因为选择让你不是直接在code更改视图的属性少了很多自由。你不能设置布局背景,不同的颜色由pressing不同的按钮,例如。

An alternative, less robust solution comes from this post. You can assign a selector for the parent layout background in xml, then in the OnTouchListener mentioned above, run setPressed(true) on that layout. It's not a great solution because the selector gives you a lot less freedom than directly changing the view's properties in code. You couldn't set the layout background to a different color by pressing a different button, for example.

感谢那些谁帮忙解决建议,在此!

Thanks to those who helped out with suggestions on this!

这篇关于如何强制以期code下一行之前立即重绘执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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