使用2种不同的布局重用Android Listview中的视图

jam*_*s0n 39 java android android-layout android-listview

我已经了解到,为了最大限度地提高Android列表视图的效率,您应该只需要尽可能多的充满"行"视图,以适应屏幕.一旦视图移出屏幕,您应该在getView方法中重复使用它,检查是否convertView为null.

但是,当您需要2个不同的列表布局时,如何实现这个想法?让我们说它的订单列表和1个布局是针对已完成的订单而另一个布局是针对流程订单.

这是我的代码使用的想法的示例教程.在我的情况下,我将有2行布局:R.layout.listview_item_product_completeR.layout.listview_item_product_inprocess

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

ViewHolder holder = null;

if (convertView == null) {
    holder = new ViewHolder();
    if(getItemViewType(position) == COMPLETE_TYPE_INDEX) {
        convertView = mInflator.inflate(R.layout.listview_item_product_complete, null);
        holder.mNameTextView = (TextView) convertView.findViewById(R.list.text_complete);
        holder.mImgImageView = (ImageView) convertView.findViewById(R.list.img_complete);
    }
    else { // must be INPROCESS_TYPE_INDEX
        convertView = mInflator.inflate(R.layout.listview_item_product_inprocess, null);
        holder.mNameTextView = (TextView) convertView.findViewById(R.list.text_inprocess);
        holder.mImgImageView = (ImageView) convertView.findViewById(R.list.img_inprocess);
    }
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}
    thisOrder = (Order) myOrders.getOrderList().get(position);
    // If using different views for each type, use an if statement to test for type, like above
    holder.mNameTextView.setText(thisOrder.getNameValue());
    holder.mImgImageView.setImageResource(thisOrder.getIconValue());
    return convertView;
}

public static class ViewHolder {
    public TextView mNameTextView;
    public ImageView mImgImageView;
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 84

您需要让适配器的视图回收器知道有多个布局以及如何区分每行的两个布局.只需覆盖这些方法:

@Override
public int getItemViewType(int position) {
    // Define a way to determine which layout to use, here it's just evens and odds.
    return position % 2;
}

@Override
public int getViewTypeCount() {
    return 2; // Count of different layouts
}
Run Code Online (Sandbox Code Playgroud)

合并getItemViewType()在里面getView(),像这样:

if (convertView == null) {
    // You can move this line into your constructor, the inflater service won't change.
    mInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    if(getItemViewType(position) == 0)
        convertView = mInflater.inflate(R.layout.listview_item_product_complete, parent, false);
    else
        convertView = mInflater.inflate(R.layout.listview_item_product_inprocess, parent, false);
    // etc, etc...
Run Code Online (Sandbox Code Playgroud)

观看Android的Romain Guy 在Google Talks上讨论视图回收器.

  • "就我而言,两种布局都有相同的字段类型(格式不同).所以我只需要一个持有者." 是的,这是对的."如果一个布局有TextView和ImageView,而另一个布局有2个TextView,我需要2个不同的持有者,不是吗?" 您可以拥有两个不同的ViewHolder,也可以组合ViewHolders,但只能访问每个布局的相应成员.那有意义吗? (2认同)

And*_*ndy 9

无需自己设计解决方案,只需覆盖getItemViewType()和getViewTypeCount().

有关示例http://sparetimedev.blogspot.co.uk/2012/10/recycling-of-views-with-heterogeneous.html,请参阅以下博客文章.

正如博客解释的那样,Android实际上并不保证 getView会收到正确的视图类型.