我在项目中覆盖了getItemViewType()方法,以指示要用于列表中的项目(R.layout.listview_item_product_completeR.layout.listview_item_product_inprocess)的视图

我知道此函数返回的值必须比可能的视图数(在我的情况下为0或1)少0到1之间。

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



@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的值?

最佳答案

我需要知道如何将COMPLETE_TYPE_INDEX定义为1或0。这看起来很琐碎!


老实说,COMPLETE_TYPE_INDEX为0和INPROCESS_TYPE_INDEX为1无关紧要,反之亦然。但是您将它们定义为类变量,在这种情况下,它们也可以是staticfinal

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

10-07 22:18