0

I have following code that works fine when my ListView is already rendered (eg. when fired by onClick events, etc.)

TextView tv = (TextView)list.getChildAt(position);  //list is my ListView
if (tv!=null) {
    tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}

However, sometimes I need to apply this STRIKE_THRU_TEXT_FLAG flag to some items of ListView when activity is being restored (after rotation, restart...). If I try to run this code in onCreate or onStart methods then list.getChild(position) returns null because no ListView item is visible yet (in fact screen is black at this time and actual drawing seems to be done in some later function).

Is there any easy way how to get around this? Maybe getChild function is not the best for this case...Or would you override rendering function of the ListView to make it work (seems like overkill to me)? Thanks

4

1 回答 1

0

不要那样修改视图本身;修改表示该列表项的实际数据对象(例如,如果您的 ListView 由 ArrayAdapter 支持,则这将是该列表位置的数组项)。您可以设置一个标志或属性来表示该项目应该以不同的方式显示,然后将一些条件代码添加到您的适配器的getView()方法中,以根据该列表项对象的属性以适当的样式显示文本。

然后,您可以让视图在下次显示时相应地呈现,或者通过调用notifyDataSetChanged()列表适配器自己触发重绘。

于 2013-01-03T20:18:36.823 回答