java - 在具有 2 种不同布局的 Android Listview 中重用 View

标签 java android android-layout android-listview

我了解到,要最大限度地提高 Android ListView 的效率,您应该只拥有尽可能多的膨胀“行” View 以适应屏幕。一旦 View 移出屏幕,您应该在 getView 方法中重用它,检查 convertView 是否为空。

但是,当您需要 2 种不同的列表布局时,如何实现这个想法?假设它是一个订单列表,其中一个布局用于完成订单,另一个布局用于处理中的订单。

这是我的代码使用的想法的示例教程。就我而言,我将有 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;
}

最佳答案

您需要让适配器的 View 回收器知道存在多个布局以及如何区分每一行的两者。只需覆盖这些方法:

@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
}

getView() 中加入 getItemViewType(),如下所示:

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...

观看 Android 的 Romain Guy discuss the view recycler在 Google Talks 上。

关于java - 在具有 2 种不同布局的 Android Listview 中重用 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13297299/

相关文章:

java - 使用 GridBagLayout 调整大小不当

java - 在 Camunda 中为流程实现更复杂的条件

android - 将图像从安卓客户端发送到谷歌应用引擎

android - 以编程方式在 Android 中使 TextView 可滚动

android - StateFlow 和 LazyColumn 重组

android-layout - Android 平板电脑应用程序中的 ListFragment,不使用全屏?

java - 如何修复我的对话框暗淡和 Alpha 效果

java - 尝试在表刷新后保留排序器位置

android - 资源和布局方向仅在 Android 8.0 及更高版本上呈现不正确

java - 进程完成并运行后看不到 GUI 的输出