1

I am trying to create a Custom Adapter which can handle any kind of layout to be inflated. By generic I mean any kind of data to be set in an adapter and any kind of a layout could be used with Event Listeners to be set for an item.

For example:

I have a contact list and a country list. The Contact list has four items in its layout i.e person image, name, number and a checkBox. The Country List contains a CountryName(TextView) and a checkbox.

Is it possible for both to use the same adapter and that adapter handle all kind of items?

I wish the tick images would reset after clicking on any item.

4

1 回答 1

3

如果您有一个包含不同对象的列表,该对象可以是不同类型的并且需要不同的视图来显示。这样做:

让对象自己定义视图。ViewProvider在每个对象上实现一个接口。这个接口应该提供getView()可以在适配器中调用的方法。

适配器现在只需将元素从列表中取出ViewProviders并调用方法getView来获取视图。

您不必担心视图的回收,因为视图存储在每个视图中ViewProvider并且只会创建一次。然后也可以在对象端而不是在适配器中更新字段(如果有)。但是您必须通过调用通知适配器有关数据更改notifyDataSetChanged()

于 2012-07-27T16:39:01.520 回答