ExpandableListView Only Last 上的自定义指标正在发生变化
Custom Indicator on ExpandableListView Only Last is changing
我正在尝试使用自定义指标实现 ExpandableListView
。
该列表有两组,每组有 3 个孩子。
问题是当我点击 group1 时,指示器没有改变,但如果我点击 group2,指示器工作正常。
我不知道怎么回事。
我也想了解更多关于getTag()
和setTag()
的信息。如何访问特定的 GroupView
或 ChildView
.
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.group_view, null);
groupname = (TextView) convertView.findViewById(R.id.groupNamesTV);
imageView = (ImageView) convertView.findViewById(R.id.indicator);
convertView.setTag(new ViewHolderForGroup(groupname , imageView));
group = (ViewHolderForGroup) convertView.getTag();
}
if(isExpanded)
{
imageView.setImageResource(R.mipmap.down);
}else {
imageView.setImageResource(R.mipmap.right);
}
group.txtView.setText(groupnames.get(groupPosition));
return convertView;
}
你错过了 convertView 不为 null 的情况,你需要在这种情况下分配 imageView :)
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.group_view, null);
groupname = (TextView) convertView.findViewById(R.id.groupNamesTV);
imageView = (ImageView) convertView.findViewById(R.id.indicator);
convertView.setTag(new ViewHolderForGroup(groupname , imageView));
group = (ViewHolderForGroup) convertView.getTag();
} else {
// we have a view previously created, our stuff is in the TAG
group = (ViewHolderForGroup) convertView.getTag();
imageView = group.imageView; // or whatever its name is
}
if(isExpanded)
{
imageView.setImageResource(R.mipmap.down);
}else {
imageView.setImageResource(R.mipmap.right);
}
group.txtView.setText(groupnames.get(groupPosition));
return convertView;
}
这里的关键点是,convertview==null
是最坏的情况,此时还没有创建视图。然后,您正确地创建了所有内容并将其存储在 TAG 中。 标签就像一个盒子,您可以在其中存放任何您想要的东西。
以后系统需要另外一个视图时,convertView不会为null,然后就是访问TAG中之前存储的东西,恢复imageView
。
您可以像这样进一步优化您的例程:
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
// all this is very slow, that's why we only do it when
// convertView is null, because it's common to all items.
// once a view is created, we will reuse this to show another row
convertView = inflater.inflate(R.layout.group_view, null);
TextView textGroup = (TextView) convertView.findViewById(R.id.groupNamesTV);
ImageView imgGroup = (ImageView) convertView.findViewById(R.id.indicator);
convertView.setTag(new ViewHolderForGroup(textGroup, imgGroup));
}
// regardless of convertView is newly created or recycled, the TAG will now always contain the ViewHolder
ViewHolderForGroup groupHolder = (ViewHolderForGroup) convertView.getTag();
groupHolder.imageView.setImageResource(isExpanded?R.mipmap.down:R.mipmap.right);
groupHolder.txtView.setText(groupnames.get(groupPosition));
return convertView;
}
你看我已经将 imageView、group 和 groupname 切换为局部变量而不是成员:这样你就不可能 运行 像以前一样出错。
我正在尝试使用自定义指标实现 ExpandableListView
。
该列表有两组,每组有 3 个孩子。
问题是当我点击 group1 时,指示器没有改变,但如果我点击 group2,指示器工作正常。
我不知道怎么回事。
我也想了解更多关于getTag()
和setTag()
的信息。如何访问特定的 GroupView
或 ChildView
.
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.group_view, null);
groupname = (TextView) convertView.findViewById(R.id.groupNamesTV);
imageView = (ImageView) convertView.findViewById(R.id.indicator);
convertView.setTag(new ViewHolderForGroup(groupname , imageView));
group = (ViewHolderForGroup) convertView.getTag();
}
if(isExpanded)
{
imageView.setImageResource(R.mipmap.down);
}else {
imageView.setImageResource(R.mipmap.right);
}
group.txtView.setText(groupnames.get(groupPosition));
return convertView;
}
你错过了 convertView 不为 null 的情况,你需要在这种情况下分配 imageView :)
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.group_view, null);
groupname = (TextView) convertView.findViewById(R.id.groupNamesTV);
imageView = (ImageView) convertView.findViewById(R.id.indicator);
convertView.setTag(new ViewHolderForGroup(groupname , imageView));
group = (ViewHolderForGroup) convertView.getTag();
} else {
// we have a view previously created, our stuff is in the TAG
group = (ViewHolderForGroup) convertView.getTag();
imageView = group.imageView; // or whatever its name is
}
if(isExpanded)
{
imageView.setImageResource(R.mipmap.down);
}else {
imageView.setImageResource(R.mipmap.right);
}
group.txtView.setText(groupnames.get(groupPosition));
return convertView;
}
这里的关键点是,convertview==null
是最坏的情况,此时还没有创建视图。然后,您正确地创建了所有内容并将其存储在 TAG 中。 标签就像一个盒子,您可以在其中存放任何您想要的东西。
以后系统需要另外一个视图时,convertView不会为null,然后就是访问TAG中之前存储的东西,恢复imageView
。
您可以像这样进一步优化您的例程:
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
// all this is very slow, that's why we only do it when
// convertView is null, because it's common to all items.
// once a view is created, we will reuse this to show another row
convertView = inflater.inflate(R.layout.group_view, null);
TextView textGroup = (TextView) convertView.findViewById(R.id.groupNamesTV);
ImageView imgGroup = (ImageView) convertView.findViewById(R.id.indicator);
convertView.setTag(new ViewHolderForGroup(textGroup, imgGroup));
}
// regardless of convertView is newly created or recycled, the TAG will now always contain the ViewHolder
ViewHolderForGroup groupHolder = (ViewHolderForGroup) convertView.getTag();
groupHolder.imageView.setImageResource(isExpanded?R.mipmap.down:R.mipmap.right);
groupHolder.txtView.setText(groupnames.get(groupPosition));
return convertView;
}
你看我已经将 imageView、group 和 groupname 切换为局部变量而不是成员:这样你就不可能 运行 像以前一样出错。