[英]android ListView ViewHolder and Type
I want to have a Listview adapter where each rows uses it's own convertview, so this convertview couldn't be share to another row. 我想要一个Listview适配器,其中每一行都使用它自己的convertview,因此无法将该convertview共享到另一行。 Basically, if I have 4 rows, I would like to have 4 convertview.
基本上,如果我有4行,我想有4个convertview。 And when drawing the row #3 I should get the convertview #3
当绘制第3行时,我应该获得convertview#3
To do this I setted each row a different type (it's position) I thought it would do the trick. 为此,我为每行设置了不同的类型(位置),我认为这可以解决问题。
public class myadapter extends BaseAdapter {
final List<String> mdata;
@Override
public int getCount() {
return mdata.size();
}
@Override
public Object getItem(int p) {
return p;
}
@Override
public long getItemId(int p) {
return p;
}
@Override
public int getItemViewType(int p) {
return p;
}
@Override
public int getViewTypeCount() {
return mdata.size() == 0 ? 1 :mdata.size() + 1;
}
@Override
public View getView(int p, View v, ViewGroup arg2) {
ViewHolder holder = null;
if (v == null) {
v = li.inflate(R.layout.data, null);
holder = new ViewHolder();
holder._pos = p;
Log.v("TAG", "creating holder pos " + p + " for " + mdata.get(p));
{...}
v.setTag(holder);
} else {
holder = (ViewHolder)v.getTag();
Log.d("TAG", "get convert for " + mdata.get(p)+ " holder " + holder._pos);
}
{...}
return v;
}
public static class ViewHolder {
public TextView _textView;
public int _pos;
}
}
Note the pos in the viewholder is only here to debug purpose. 注意,viewholder中的pos仅用于调试目的。
Unfortunatly it displays: 不幸的是,它显示:
creating holder pos 0 for string0 creating holder pos 1 for string1 get convert for string0 holder 1 get convert for string0 holder 0 get convert for string1 holder 1 创建string0的持有人pos 0创建string1的持有人pos 1获取string0持有人的转换1获取string0持有人的转换0获取string1持有人的转换
How the get convert for string0 holder 1 is possible ? 如何为string0持有人1进行get转换?
Thanks 谢谢
When I copy paste your code and set following field ... 当我复制粘贴您的代码并设置以下字段时...
final List<String> mdata = Arrays.asList("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11", "s12");
... iT works the exact way you would expect it. ... iT以您期望的确切方式工作。
Are you sure you didn't missunderstand the log output? 您确定您不会错过对日志输出的了解吗?
I found the problem, 我发现问题了
getViewTypeCount()
could not change during the execution of the program. 在程序执行期间无法更改。 You have to set it to a big value, even if you use less.
即使使用较少,也必须将其设置为较大的值。 It has to be a constant.
它必须是一个常数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.