ExpandableListView上的自定义指示器最后一个正在更改

时间:2016-02-04 09:24:36

标签: java android expandablelistview

我正在尝试使用自定义指标实现ExpandableListView。 该名单有两组,每组3个孩子。

问题是,当我点击group1时,指标不会改变,但是如果我点击group2,指标就可以正常运行。

我不知道哪里出错了。

我还想了解有关getTag()setTag()的更多信息。如何访问特定的GroupViewChildView

@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;
    }

1 个答案:

答案 0 :(得分:0)

你错过了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切换为局部变量而不是成员:这样就不可能像以前一样遇到错误。