我的 ListView 有两种类型的视图.如何管理? [英] My ListView has two types of views. How to manage?

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

问题描述

我的事件管理应用在列表视图中有两种类型的视图:用于不太重要的事件的小型简单文本视图和使用 FrameLayout 创建的复杂视图.

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.

在适配器的 onCreateView() 中,我根据事件的性质返回这些视图.

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
}  

convertView 为空时,这一切都很好.由于Android中的视图回收,返回给我的convertView可能会从更简单的视图中回收,我现在必须显示更大的视图.一个愚蠢的解决方案是一直夸大新的观点.但是,这会降低性能.

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.

另一种方法是在它们(visibility=gone)中都有不可见的 TextView 和预定义的值,并根据其中的值,我可以膨胀意见.对我来说,这似乎是一种黑客行为,而不是真正的解决方案.

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.

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

推荐答案

由于Android中的视图回收,可能会发生返回给我的convertView是从更简单的视图中回收的,我现在必须显示更大的视图.

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.

如果您覆盖 getViewTypeCount()getItemViewType() 在你的适配器中.

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

就你而言:

  • getViewTypeCount() 将返回 2

getItemViewType() 将返回 0 或 1,其中对于属于一种类型行的位置返回 0,对于返回另一种类型行的位置返回 1

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

然后,您可以保证如果 convertView 不是 null,则它是正确类型的行.

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

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

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