到目前为止,我曾经使用ListView
以这种方式填充SimpleAdapter
:
// Example of a list with cells containing some text and an image
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), list, R.layout.my_cell,
new String[] {"img", "title"}, new int[] {R.id.img, R.id.title});
list.setAdapter(mSchedule);
这个解决方案效果很好,但有了它,我只能使用一种单元格,my_cell
。
有什么想法解决这个问题吗?感谢。
答案 0 :(得分:3)
您需要覆盖一些适配器方法,最重要的是:getViewTypeCount()
,getItemViewType()
和getView()
。
getViewTypeCount()
返回适配器中不同视图类型的数量
getItemViewType()
返回适配器中特定位置的视图类型
如果getView()
为convertView
,则null
必须创建正确类型的视图
如果convertView不是null
,则根据getViewTypeCount()
和getItemViewType()
之前返回的信息,它的类型是正确的。
P.S。 @Rob我所有的例子都很大,所以我会试着简单解释一下: 假设您只有2种视图类型。
getViewTypeCount()
应返回2 getItemViewType()
应返回0或1,具体取决于position
参数。此方法将在getView()
getView()
将使用与调用position
相同的getItemViewType()
参数进行调用。使用与position
中使用的基于getItemViewType()
的算法相同的算法来确定应返回哪种视图类型(您甚至可以在此处重复使用getItemViewType()
)。然后检查convertView
是否为空。