1

我正在覆盖getItemViewType()我的项目中的方法以指示要为列表中的项目使用哪个视图,R.layout.listview_item_product_complete或者R.layout.listview_item_product_inprocess

我知道这个函数必须返回一个介于 0 和 1 之间的值,小于可能的视图数,在我的例子中是 0 或 1。

我怎么知道哪个布局是 0 哪个是 1?我假设我创建的第一个布局将为 0,后一个布局为 1,但我想返回一个变量,以便该值灵活...

IE

@Override
public int getItemViewType(int position) {
    // Define a way to determine which layout to use
    if(//test for inprocess){ 
        return INPROCESS_TYPE_INDEX;
    } else { 
        return COMPLETE_TYPE_INDEX;
    }
}

我可以参考什么/在哪里定义 和 的COMPLETE_TYPE_INDEXINPROCESS_TYPE_INDEX

4

2 回答 2

2

我需要知道如何将 COMPLETE_TYPE_INDEX 定义为 1 或 0。这似乎是一件微不足道的事情!

COMPLETE_TYPE_INDEX老实说,是 0还是 1都没有关系INPROCESS_TYPE_INDEX,反之亦然。static但是您将它们定义为类变量,在这种情况下,它们也可以是final

public class MyAdapter ... {
    private static final int COMPLETE_TYPE_INDEX = 0;
    private static final int INPROCESS_TYPE_INDEX = 1;
    private static final int NUMBER_OF_LAYOUTS = 2;

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            if(getItemViewType(position) == COMPLETE_TYPE_INDEX)
                convertView = mInflater.inflate(R.layout.listview_item_product_complete, null);
            else // must be INPROCESS_TYPE_INDEX
                convertView = mInflater.inflate(R.layout.listview_item_product_inprocess, null);

            // etc, etc...
            // Depending on what is different in your layouts, 
            //   you may need update your ViewHolder and more of getView()
        }

        // Load data that changes on each row, might need to check index type here too
    }
    @Override
    public int getItemViewType(int position) {
        Order thisOrder = (Order) myOrders.getOrderList().get(position);

        if(thisOrder.getOrderStatus().equals("Complete")) return COMPLETE_TYPE_INDEX;
        else return INCOMPLETE_TYPE_INDEX;
    }
    @Override
    public int getViewTypeCount() {
        return NUMBER_OF_LAYOUTS;
    }
}
于 2012-11-09T20:40:52.020 回答
1

在你的getView方法 中

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

 if(getItemViewType(position) == INPROCESS_TYPE_INDEX){

//inflate a layout file
convertView = inflater.inflate(R.layout.R.layout.listview_item_product_inprocess);
}

else{
{

//inflate a layout file
convertView = inflater.inflate(R.layout.listview_item_product_complete);

}

return convertView;
于 2012-11-09T20:04:00.663 回答