gpt4 book ai didi

java - 使用 getItemViewType() - 我如何知道哪个布局是 0 哪个是 1?

转载 作者:行者123 更新时间:2023-11-29 05:52:43 25 4
gpt4 key购买 nike

我正在重写我项目中的 getItemViewType() 方法,以指示我的列表中的项目使用哪个 View ,R.layout.listview_item_product_complete R.layout.listview_item_product_inprocess

我知道此函数必须返回一个小于可能 View 数的 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 的值?

最佳答案

I need to know how to define COMPLETE_TYPE_INDEX as 1 or 0. Seems like such a trivial thing!

老实说,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;
}
}

关于java - 使用 getItemViewType() - 我如何知道哪个布局是 0 哪个是 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13315071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com