我的ListView中有两种意见。如何管理? [英] My ListView has two types of views. How to manage?

查看:105
本文介绍了我的ListView中有两种意见。如何管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

解决方案
  

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

如果你不重写<一个href="http://developer.android.com/reference/android/widget/Adapter.html#getViewTypeCount()"><$c$c>getViewTypeCount()和<一href="http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)"><$c$c>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.

这篇关于我的ListView中有两种意见。如何管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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