android ListView ViewHolder 和类型
我想要一个 Listview 适配器,其中每行都使用它自己的 ConvertView, 所以这个convertview无法共享到另一行。 基本上,如果我有 4 行,我想要 4 个 ConvertView。 当绘制行#3时,我应该得到convertview#3
为此,我将每一行设置为不同的类型(它的位置) 我以为这样就可以了。
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;
}
}
请注意,视图保持器中的 pos 仅用于调试目的。
不幸的是它显示:
为 string0 创建持有者 pos 0 为 string1 创建持有者 pos 1 获取 string0 持有者 1 的转换 获取 string0 持有者 0 的转换 get Convert for string1holder 1
如何 get Convert for string0holder 1?
谢谢
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.
Basically, if I have 4 rows, I would like to have 4 convertview.
And when drawing the row #3 I should get the 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.
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
How the get convert for string0 holder 1 is possible ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我复制粘贴您的代码并设置以下字段时......
iT 的工作方式与您期望的完全相同。
您确定您没有误解日志输出吗?
When I copy paste your code and set following field ...
... iT works the exact way you would expect it.
Are you sure you didn't missunderstand the log output?
我发现了问题,
程序执行过程中无法改变。
即使使用较少,也必须将其设置为较大的值。它必须是一个常数。
I found the problem,
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.