gpt4 book ai didi

android - 可扩展 ListView 适配器 getView() 重复位置

转载 作者:行者123 更新时间:2023-11-30 02:03:55 31 4
gpt4 key购买 nike

当我在下方滚动时,我的 ExpandableListView 的适配器会重复 View 和图像的位置是我的代码。我在没有 View holder 和有 View holder 的情况下使用它,在这两种情况下我都遇到了这个问题。

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

View view = convertView;
String groupName = (String) getGroup(groupPosition);

if (view == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.group_item, null);
holder = new ViewHolder();

holder.arrowImage = (ImageView) view.findViewById(R.id.imageView1);
setImageView(holder.arrowImage, groupPosition);
view.setTag(holder);
holder.groupName = (TextView) view.findViewById(R.id.laptop);
holder.groupName.setTypeface(null, Typeface.BOLD);
holder.groupName.setText(groupName);

} else {
holder = (ViewHolder) view.getTag();
holder.groupName = (TextView) view.findViewById(R.id.laptop);
holder.groupName.setTypeface(null, Typeface.BOLD);
holder.groupName.setText(groupName);
}

return view;

}

public ImageView getImageView(int position) {
return images[position];
}

public void setImageView(ImageView image, int position) {
images[position] = image;
}

static class ViewHolder {
ImageView arrowImage;
TextView groupName;
}

最佳答案

不要通过标签选择 View

使用这个

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

View view = convertView;
String groupName = (String) getGroup(groupPosition);

LayoutInflater inf = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.group_item, null);
holder = new ViewHolder();

holder.arrowImage = (ImageView) view.findViewById(R.id.imageView1);
holder.groupName = (TextView) view.findViewById(R.id.laptop);
holder.groupName.setTypeface(null, Typeface.BOLD);
holder.groupName.setText(groupName);
setImageView(holder.arrowImage, groupPosition);
view.setTag(holder);

return view;

}

public ImageView getImageView(int position) {
return images[position];
}

public void setImageView(ImageView image, int position) {
images[position] = image;
}

static class ViewHolder {
ImageView arrowImage;
TextView groupName;
}

这将防止图像重复并将每个 View 视为一个新 View

关于android - 可扩展 ListView 适配器 getView() 重复位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31050604/

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