【问题标题】:convertView in custom adapter with multiple line views具有多行视图的自定义适配器中的 convertView
【发布时间】:2014-03-20 21:32:10
【问题描述】:

我正在实现自定义适配器,它根据这个(非常有用的)教程在listview 中处理多种类型的行:http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/

现在,我以为我什么都明白了,但有一件事让我很困惑。 在 getView 方法中,我们收到 convertView ,它假设是具有特定布局的视图(组),以显示在列表视图的特定行中。

public View getView(int position, View convertView, ViewGroup parent) {
    //first get the animal from our data model
    Animal animal = animals.get(position);

    //if we have an image so we setup an the view for an image row
    if (animal.getImageId() != null) {
        ImageRowViewHolder holder;
        View view;

        //don't have a convert view so we're going to have to create a new one
        if (convertView == null) {
            ViewGroup viewGroup = (ViewGroup)LayoutInflater.from(AnimalHome.this)
                    .inflate(R.layout.image_row, null);

            //using the ViewHolder pattern to reduce lookups
            holder = new ImageRowViewHolder((ImageView)viewGroup.findViewById(R.id.image),
                        (TextView)viewGroup.findViewById(R.id.title));
            viewGroup.setTag(holder);

            view = viewGroup;
        }
        //we have a convertView so we're just going to use it's content
        else {
            //get the holder so we can set the image
            holder = (ImageRowViewHolder)convertView.getTag();

            view = convertView;
        }

        //actually set the contents based on our animal
        holder.imageView.setImageResource(animal.getImageId());
        holder.titleView.setText(animal.getName());

        return view;
    }
    //basically the same as above but for a layout with title and description
    else {
        DescriptionRowViewHolder holder;
        View view;
        if (convertView == null) {
            ViewGroup viewGroup = (ViewGroup)LayoutInflater.from(AnimalHome.this)
                    .inflate(R.layout.text_row, null);
            holder = new DescriptionRowViewHolder((TextView)viewGroup.findViewById(R.id.title),
                    (TextView)viewGroup.findViewById(R.id.description));
            viewGroup.setTag(holder);
            view = viewGroup;
        } else {
            view = convertView;
            holder = (DescriptionRowViewHolder)convertView.getTag();
        }

        holder.descriptionView.setText(animal.getDescription());
        holder.titleView.setText(animal.getName());

        return view;
    }
}

但是,如果listview 中有多种类型的行(例如,带有分隔符的动物列表,标题为“mamals”、“fish”、“birds”),listview 如何知道发送什么convertView?它可以是两种完全不同的类型之一。有些事情对我来说很不清楚。有人可以解释一下吗?

【问题讨论】:

  • 阅读您在问题中提到的整个教程。你会找到答案的!

标签: android listview custom-adapter convertview


【解决方案1】:

根据您提供的教程:)

android 适配器提供的用于管理不同行类型的另外两种方法是:

getItemViewType(int position)getViewTypeCount()。 列表视图使用这些方法创建不同的视图池以重复用于不同类型的行。

祝你好运:)

【讨论】:

  • 伙计,在跳代码之前我应该​​学会更仔细地阅读:-) 非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
相关资源
最近更新 更多