使用不同的项目布局为ListView创建ViewHolders

时间:2010-08-18 16:51:01

标签: android listview listactivity

我有一个ListView,具有不同的布局,适用于不同的项目。有些项目是分隔符。有些项目不同,因为它们包含不同类型的数据等。

我想实现ViewHolders来加速getView进程,但我不太清楚如何去做。不同的布局具有不同的数据(这使得命名困难)和我想要使用的不同数量的视图。

我该怎么做呢?

我能想到的最好的想法是创建一个带X项的通用ViewHolder,其中X是项目布局中具有最多数量的视图数。对于具有少量视图的其他视图,我将在ViewHolder中使用这些变量的子部分。所以说我有2个布局我用于2个不同的项目。一个有3个TextViews,另一个有1.我会创建一个带有3个TextView变量的ViewHolder,并且只使用其中一个用于我的其他项目。我的问题是,这可能会变得非常难看,感觉非常黑客;特别是当项目布局可能有许多不同类型的视图时。

这是一个非常基本的getView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    MyHolder holder;

    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.layout_mylistlist_item, parent, false);

        holder = new MyHolder();
        holder.text = (TextView) v.findViewById(R.id.mylist_itemname);
        v.setTag(holder);
    }
    else {
        holder = (MyHolder)v.getTag();
    }

    MyListItem myItem = m_items.get(position);

    // set up the list item
    if (myItem != null) {
        // set item text
        if (holder.text != null) {
            holder.text.setText(myItem.getItemName());
        }
    }

    // return the created view
    return v;
}

假设我有不同类型的行布局,我可以为每种类型的行设置ViewHolder。但是我会说什么类型的“持有者”在顶部?或者我会为每种类型声明一个持有者,然后使用那个我所在行的类型。

3 个答案:

答案 0 :(得分:109)

ListView有一个内置的类型管理系统。在适配器中,您有多种类型的项目,每个项目都有自己的视图和布局。通过重写getItemViewType来返回给定位置的数据类型,ListView很愿意为该类型的数据传递正确的convertview。然后,在getView方法中,只需检查数据类型并使用switch语句以不同方式处理每种类型。

每种布局类型都应有自己的视图,以便命名清晰,易于维护。将ViewHolders命名为与每种数据类型相关的内容,以保持一切顺利。

尝试将所有内容重叠到一个ViewHolder中是不值得的。

修改的 实施例

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    int viewType = this.getItemViewType(position);

    switch(viewType)
    {
       case TYPE1:

        Type1Holder holder1; 

         View v = convertView; 
         if (v == null) { 
             LayoutInflater vi = (LayoutInflater)getContext().getSystemService     (Context.LAYOUT_INFLATER_SERVICE); 
             v = vi.inflate(R.layout.layout_mylistlist_item_type_1, parent, false); 

             holder1 = new Type1Holder (); 
             holder1.text = (TextView) v.findViewById(R.id.mylist_itemname); 
             v.setTag(holder1); 
         } 
         else { 
             holder1 = (Type1Holder)v.getTag(); 
         } 

         MyListItem myItem = m_items.get(position); 

         // set up the list item 
         if (myItem != null) { 
             // set item text 
             if (holder1.text != null) { 
                 holder1.text.setText(myItem.getItemName()); 
             } 
         } 

         // return the created view 
         return v; 


     case TYPE2:
            Type2Holder holder2; 

         View v = convertView; 
         if (v == null) { 
             LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
             v = vi.inflate(R.layout.layout_mylistlist_item_type_2, parent, false); 

             holder2 = new Type2Holder (); 
             holder2.text = (TextView) v.findViewById(R.id.mylist_itemname); 
             holder2.icon = (ImageView) v.findViewById(R.id.mylist_itemicon); 
             v.setTag(holder1); 
         } 
         else { 
             holder2 = (Type2Holder)v.getTag(); 
         } 

         MyListItem myItem = m_items.get(position); 

         // set up the list item 
         if (myItem != null) { 
             // set item text 
             if (holder2.text != null) { 
                 holder2.text.setText(myItem.getItemName()); 
             } 

             if(holder2.icon != null)
                 holder2.icon.setDrawable(R.drawable.icon1);
         } 


         // return the created view 
         return v; 


       default:
           //Throw exception, unknown data type
    }
} 

答案 1 :(得分:0)

另一个例子。

公共类CardArrayAdapter扩展了ArrayAdapter {

public CardArrayAdapter(Context context) {
    super(context, R.layout.adapter_card);
}

@Override
public View getView(int position, View view, ViewGroup parent) {

    final Card card = getItem(position);
    ViewHolder holder;

    //if (view != null) {
        //holder = (ViewHolder) view.getTag();
    //} else {
    Log.d("card.important?", card.name + " = " + Boolean.toString(card.important));
    if(card.important) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.adapter_card_important, parent, false);
    }else {
        view = LayoutInflater.from(getContext()).inflate(R.layout.adapter_card, parent, false);
    }
    holder = new ViewHolder(view);
    view.setTag(holder);
    //}

    // IMG
    Picasso.with(getContext())
            .load(card.logo)
            .placeholder(R.drawable.ic_phonebook)
            .error(R.drawable.ic_phonebook)
            .fit()
            .centerCrop()
            .transform(new CircleTransform())
            .into(holder.logo);

    holder.name.setText(card.name);

    if(card.important) {
        holder.category.setVisibility(View.VISIBLE);
        if (card.category.equals("airline")) {
            card.category = "airlines";
        }
        int id = getContext().getResources().getIdentifier(card.category, "string", getContext().getPackageName());
        holder.category.setText(getContext().getResources().getString(id));
    }else {
        holder.category.setVisibility(View.GONE);
    }

    holder.tagline.setText(card.tagline);
    holder.favorite.setChecked(card.favorite);

    holder.favorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            card.favorite = ((CheckBox) v).isChecked();
            card.save();
        }
    });

    return view;

}

static class ViewHolder {
    @InjectView(R.id.logo) ImageView logo;
    @InjectView(R.id.name) TextView name;
    @InjectView(R.id.category) TextView category;
    @InjectView(R.id.tagline) TextView tagline;
    @InjectView(R.id.favorite) CheckBox favorite;
    public ViewHolder(View view) {
        ButterKnife.inject(this, view);
    }
}

}

答案 2 :(得分:0)

使用视图类型并不是最简单的方法。有时最好不要使用 ViewType,而是实现一个可以完成所有事情的类层次结构。

好的,我们有一个任务来显示列表中的家具项目 - 椅子、床等等。先实现对象模型:

public abstract class FurnitureBase  {
    @LayoutRes
    abstract public int getLayoutFileResource();
    abstract public HolderFurnitureBase getHolder(View convertView);
}


public class FurnitureChair extends FurnitureBase  {
    public double price;
    public Material material;
    ...

    public FurnitureChair(double price, Material material) {
        ...
    }


    @Override
    public int getLayoutFileResource() {
        return R.layout.item_furniture_chair;
    }


    @Override
    public HolderFurnitureBase getHolder(View convertView) {
        return new HolderFurnitureChair(convertView);
    }
}


public class FurnitureBed extends FurnitureBase  {
    public double price;
    public BedSize size;
    ...

    public FurnitureBed(double price, BedSize size) {
        ...
    }


    @Override
    public int getLayoutFileResource() {
        return R.layout.item_furniture_bed;
    }


    @Override
    public HolderFurnitureBase getHolder(View convertView) {
        return new HolderFurnitureBed(convertView);
    }
}

接下来,创建持有人:

public abstract class HolderFurnitureBase
{
    public HolderFurnitureBase(View convertView) { };

    public abstract void renderItem(FurnitureBase item);
}

public class HolderFurnitureChair extends HolderFurnitureBase
{
    private final ImageViewAccent mIconAction;
    private final TextViewPrimaryDark mPrice;
    ...

    public HolderFurnitureChair(View convertView)
    {
        // just init views
        super(convertView);
        this.mIconAction = convertView.findViewById(R.id.item_furniture_chair_icon_action;
        this.mPrice = convertView.findViewById(R.id.item_furniture_chair_text_price);
    }


    public void renderItem(FurnitureBase item)
    {
        FurnitureChair chair = (FurnitureChair ) item;
        mIconAction.setImageResource(chair.getProductTypeIcon());
        mPrice.setText(Utils.Formatter.formatMoney(chair.price, chair.priceCurrency));
    }
}


public class HolderFurnitureBed extends HolderFurnitureBase
{
    private final TextView mSize;
    private final TextViewPrimaryDark mPrice;
    ...

    public HolderFurnitureBed(View convertView)
    {
        // just init views
        super(convertView);
        this.mSize = convertView.findViewById(R.id.item_furniture_bed_text_size;
        this.mPrice = convertView.findViewById(R.id.item_furniture_bed_text_price);
    }


    public void renderItem(FurnitureBase item)
    {
        FurnitureBed bed = (FurnitureBed) item;
        mSize.setText(bed.getSizeText());
        mPrice.setText(Utils.Formatter.formatMoney(bed.getPrice(), bed.getPriceCurrency()));
    }
}

并收集适配器中的所有魔法:

public final class AdapterFurniture extends ArrayAdapter<FurnitureBase>
{
    public AdapterFurniture(Context context, List<FurnitureBase> items) {
        super(context, R.layout.item_furniture_bed, items);
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent)
    {
        FurnitureBase item = getItem(position);
        HolderFurnitureBase holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(item.getLayoutFileResource(), parent, false);
            holder = item.getHolder(convertView);
        }
        else {
            holder = (HolderFurnitureBase) convertView.getTag();
        }
        holder.renderItem(getItem(position));
        convertView.setTag(holder);
        return convertView;
    }
}

仅此而已。无需计算视图类型,添加沙发和扶手椅时无需更改适配器,等等 - 只需扩展新项目的基类和新持有人的持有人基类,应用程序已准备就绪测试者享受:)