ArrayAdapter的getViewTypeCount和getItemViewType方法 [英] getViewTypeCount and getItemViewType methods of ArrayAdapter

查看:95
本文介绍了ArrayAdapter的getViewTypeCount和getItemViewType方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以以纯的话解释了我的<$的 getViewTypeCount() getItemViewType()方法的用法C $ C> ArrayAdapter ?

Can somebody in plain words explain me the usage of getViewTypeCount() and getItemViewType() methods of ArrayAdapter?

推荐答案

这些处理你想要的视图不同行不同的类型的情况。例如,在联系人应用可能要偶数行具有在左侧和奇数行的图片有在右侧的图片。在这种情况下,你可以使用:

These handle the case where you want different types of view for different rows. For instance, in a contacts application you may want even rows to have pictures on the left side and odd rows to have pictures on the right. In that case, you would use:

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position % 2;
}

框架使用您的视图类型来决定的其意见,交给你通过 convertView getView 方法。换言之,在上述例子中,您的偶数行只会得到回收用图片次左侧重用,并且奇数行只会得到那些与在右侧的图片。

The framework uses your view type to decide which views to hand you via convertView in your getView method. In other words, in the above example, your even rows will only get recycled views with pictures on the left side to reuse, and odd rows will only get ones with pictures on the right.

如果您的列表中每一行都有相同的布局,你不必担心视图类型。事实上,<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/BaseAdapter.java;h=532fd766ec66ae54a6e4b3def4b8bdc839c1db7a;hb=refs/heads/master">BaseAdapter.java为所有适配器默认行为:

If every row in your list has the same layout, you don't need to worry about view types. In fact, BaseAdapter.java provides a default behavior for all adapters:

public int getItemViewType(int position) {
    return 0;
}

public int getViewTypeCount() {
    return 1;
}

这的确提供了相同的看法类型的每一行。

This indeed provides you with the same view type for every row.

修改 - 概述的一般流程:

Edit - to outline the general flow:

  1. 您将数据绑定到你的适配器视图使用适配器。
  2. 适配器视图尝试显示是对用户可见的项目。
  3. 在该框架调用 getItemViewType 中行 N ,该行是要显示。
  4. 在该框架检查其回收的意见池看法行 N 的类型。它没有找到任何因为没有意见已回收呢。
  5. getView 被要求行 N
  6. 您拨打 getItemViewType 中行 N 来确定应该使用什么样的看法。
  7. 您使用if / switch语句可以填充不同的XML文件,这取决于视图类型是必需的。
  8. 您填写的信息视图。
  9. 您退回的观点,退出 getView ,和你行的视图显示给用户。
  1. You bind data to your AdapterView using an adapter.
  2. The AdapterView tries to display items that are visible to the user.
  3. The framework calls getItemViewType for row n, the row it is about to display.
  4. The framework checks its recycled views pool for views of row n's type. It doesn't find any because no views have been recycled yet.
  5. getView is called for row n.
  6. You call getItemViewType for row n to determine what type of view you should use.
  7. You use an if/switch statement to inflate a different xml file depending on which view type is required.
  8. You fill the view with information.
  9. You return the view, exiting getView, and your row's view is displayed to the user.

现在,当一个视图循环滚动关闭它进入一个由框架管理的意见回收池中的画面。在这些由视图类型基本组织,以便正确类型的观点是给你的 convertView 参数的 getView 方法:

Now, when a view is recycled by scrolling off the screen it goes into a recycled views pool that is managed by the framework. These are essentially organized by view type so that a view of the correct type is given to you in convertView parameter in your getView method:

  1. 在该框架再次呼吁 getItemViewType 它要显示的行。
  2. 在这个时候,有一个在适当类型的循环池的美景。
  3. 在该回收的观点传递给你的 convertView 参数给你的 getView 方法。
  4. 您填写新的信息的循环观,并返回它。
  1. The framework again calls getItemViewType for the row it wants to display.
  2. This time, there is a view in the recycled pool of the appropriate type.
  3. The recycled view is passed to you as the convertView parameter to your getView method.
  4. You fill the recycled view with new information and return it.

这篇关于ArrayAdapter的getViewTypeCount和getItemViewType方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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