我的ListView中有两种意见。如何管理?我的、两种、中有、意见

由网友(蓝莓味的酸丁子)分享简介:我的活动管理的应用程序有两种类型的视图列表视图:对于不那么重要的事件,一个小simmple文本视图和创建一个复杂的视图的FrameLayout 。 在适配器onCreateView(),我回这些看法取决于事件的性质。 如果(convertView == NULL){如果(重要)//膨胀的复杂视图其他//膨胀的简单看法...

我的活动管理的应用程序有两种类型的视图列表视图:对于不那么重要的事件,一个小simmple文本视图和创建一个复杂的视图的FrameLayout

适配器onCreateView(),我回这些看法取决于事件的性质。

 如果(convertView == NULL){
  如果(重要)
   //膨胀的复杂视图
  其他
   //膨胀的简单看法
}
 

这是所有好当 convertView 为空。由于视图回收Android的,可能发生的 convertView 返回我是从简单的看法回收,我现在要显示大图。一个愚蠢的解决方案是新的充气意见所有的时间。但是,这会杀了性能。

另一种方式是有无形的的TextView S在他们两人(知名度=不见了)与predefined值,并根据什么样的价值是他们的,我可以膨胀的意见。对我来说,这似乎是一个黑客,而不是真正解决问题。

什么是正确的方式来处理这种情况?

解决方案   

由于视图回收Android的,可能发生的convertView还给我从简单的看法回收,我现在要显示大图。

如果你不重写getViewTypeCount()和getItemViewType()在您的适配器。

人类高质量安全管理工作者建议 提升外委队伍自主管理水平,必须抓好这件事

在你的情况:

getViewTypeCount()将返回2

getItemViewType()将返回0或1,在那里你返回0的位置是一类行,返回1为那些归仓另一种类型的行

然后,你保证,如果 convertView 不是,这是一排正确的类型。

My events management app has two types of views in the list view : a small simmple text view for not-so-important events and a complex view created with a FrameLayout.

in the onCreateView() of the adapter, I return these views depending upon the nature of the event.

if(convertView == null){
  if(important)
   // inflate the complex view
  else
   // inflate the simpler view
}  

This is all good when convertView is null. Due to the view recycling in Android, it may happen that the convertView returned to me is recycled from the simpler view and I now have to display the larger view. One dumb solution is to inflate new views all the time. However, this will kill performance.

Another way is to have invisible TextViews in both of them (visibility=gone) with predefined values and depending on what value is in them, I can inflate the views. To me, this seems like a hack rather than a real solution.

What is the right way to handle this situation?

解决方案

Due to the view recycling in Android, it may happen that the convertView returned to me is recycled from the simpler view and I now have to display the larger view.

Not if you override getViewTypeCount() and getItemViewType() in your adapter.

In your case:

getViewTypeCount() would return 2

getItemViewType() would return 0 or 1, where you return 0 for those positions that are one type of row, and return 1 for those positions that return the other type of row

Then, you are guaranteed that if convertView is not null, it is a row of the proper type.

阅读全文

相关推荐

最新文章