我在android中有一个GridView,我用从xml资源中检索的数据填充它
例如,我在GridView中有15个项目按顺序放置。整体高度超过了屏幕高度,所以我必须滚动才能看到剩下的项目
问题是当我向上滚动时,不可见行的顺序已经改变。这是一种神秘的行为,因为有时项目互相交换行。
这是我的getView
方法:
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c, NodeList cuu) {
cu = cuu;
}
public int getCount() {
Log.d("Node Count",cu.getLength()+"");
return cu.getLength();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
if (convertView == null) {
Node nd = cu.item(position);
Log.d("nodes","Pos: "+(position)+" Name: "+nd.getNodeName()+" Title: "+nd.getAttributes().getNamedItem("title").getTextContent());
int catID = Integer.parseInt(nd.getAttributes().getNamedItem("id").getTextContent());
LayoutInflater li = getLayoutInflater();
myView = li.inflate(R.layout.grid_item, null);
ImageView imageView = (ImageView) myView.findViewById(R.id.grid_item_image);
myView.setLayoutParams(new GridView.LayoutParams(70, 100));
id.download(nd.getAttributes().getNamedItem("icon").getTextContent(),imageView);
TextView textView = (TextView) myView.findViewById(R.id.grid_item_text);
textView.setText(nd.getAttributes().getNamedItem("title").getTextContent());
myView.setTag((Object) catID);
}else{
//Log.d("nodes","Pos: "+(position));
}
return myView;
}
private NodeList cu = null;
}
更新:嗯,这很奇怪。经过一些调试后,我注意到在GridView中,适配器跳过了第13个位置,这意味着它返回1而不是13,然后继续前进到14! (我猜13号运气不好!)
答案 0 :(得分:11)
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view= inflate your xml
} else {
view=convertView;
}
// all remaining code like
// view.findViewById(R.id.btn).setText("MyButton");
// must be outside if-else
}
答案 1 :(得分:7)
如果这是您在getView
方法中拥有的所有代码,那么您没有正确实施它:
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
if (myView == null) {
Node nd = cu.item(position);
int catID = Integer.parseInt(nd.getAttributes().getNamedItem("id")
.getTextContent());
LayoutInflater li = getLayoutInflater();
myView = li.inflate(R.layout.grid_item, null);
myView.setLayoutParams(new GridView.LayoutParams(70, 100));
myView.setTag((Object) catID);
}
Node nd = cu.item(position);
Log.d("nodes", "Pos: " + (position) + " Name: " + nd.getNodeName()
+ " Title: "
+ nd.getAttributes().getNamedItem("title").getTextContent());
ImageView imageView = (ImageView) myView
.findViewById(R.id.grid_item_image);
id.download(nd.getAttributes().getNamedItem("icon")
.getTextContent(), imageView);
TextView textView = (TextView) myView
.findViewById(R.id.grid_item_text);
textView.setText(nd.getAttributes().getNamedItem("title")
.getTextContent());
return myView;
}
我不知道上面的代码是否有效,你的代码有点奇怪。无论如何,我认为你看到它的正常行为,因为你在适配器中所做的只是填充第一个可见元素然后当你向上和向下滚动时,适配器将重新使用精确相同的元素回收。在getView
你应该:
convertView
是否为null
:
null
,则需要为此View
元素充气GridView
。您还可以使用持有者模式缓存查找撰写Views
(而不是每次都使用findViewById
进行搜索)(对于膨胀的视图使用setTag
元素,但它是一段数据来自Node
数据元素?!?您打算用它做什么?!?)null
你什么都不做(或者如果你实现了持有人模式,那么你将获得已经搜索过的Views
的标签)这就是你应该在if / else语句中做的事情
Views
(以便他们保存该位置的适当数据)。答案 2 :(得分:3)
您的适配器实施存在致命缺陷。
在getView()
中,如果convertView
为null
,则会对布局进行充气并填充其小部件。这很好。
在getView()
中,如果convertView
不是null
,则绝对不做任何事情。这是非常错误的。
如果getView()
不是convertView
, null
表示要回收的单元格,因此您可以跳过通货膨胀并节省CPU时间,但仍需要更新该单元格的小部件以反映您应该在此特定{{1}中设置的任何位置打电话。
答案 3 :(得分:2)
我刚刚删除了
if(convertView==null)
它有效
答案 4 :(得分:1)
如果您的网格项中包含多个视图,则可以使用getTag()
和setTag()
方法。在这种情况下,网格项目不再会在滚动时改变它们的位置,性能仍然很好:
public class SomeAdapter extends BaseAdapter {
public static class ViewHolder {
public TextView tvTitle;
public ImageView imgPoster;
}
//...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder grid;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
grid = new ViewHolder();
convertView = inflator.inflate(R.layout.grid_item, null);
grid.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
grid.imgPoster = (ImageView) convertView.findViewById(R.id.img_poster);
convertView.setTag(grid); // <<-- H E R E
} else {
grid = (ViewHolder) convertView.getTag(); // <<-- H E R E
}
grid.tvTitle.setText(dataItem.getTitle());
grid.imgPoster.setImage(dataItem.getImage());
return convertView;
}
}
答案 5 :(得分:0)
我使用此解决方案,基于另一个样本,并且工作:
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
view = inflate your xml and apply the View Holder pattern
} else {
view = convertView;
}
// retrieve the object by Holder pattern
// extra code here
}