调用OnChildClickListener后,getChildView中的ImageView不再更新

时间:2013-07-03 12:06:02

标签: android android-listview android-imageview expandablelistview

现在我遇到了关于expandablelistview的问题。问题是onChildClickListener和getChildView之间似乎存在冲突。

简单地说:当点击一个groupitem时会弹出两个子菜单。在这些(2)子菜单中,存在加载图像的图像视图。如果单击其中一个项目,则会生成另一个imageview - 这次是从onChildClickListener方法生成的。到现在为止还挺好。但是 - 如果继续在应用程序中扩展另一个groupitem - 从onChildClickListener方法生成的imageview仍然存在(不是来自getChildView的imageview)。

我在堆栈上发现了一个类似的问题,你可以在这里找到。但问题出现在listener和getGroupView之间。

custom views not updating with OnGroupClickListener on ExpandableListView

这是什么原因?它与缓存有什么关系吗?有什么建议如何解决这些问题?我尝试了几种方法...... invalidate(),invalidateviews(),setWillNotCacheDrawing(true),setItemsCanFocus(false)等等。

总结一下:当调用onchildclicklistener时 - 不再加载来自getchildview的图像 - 只有来自监听器的图像。

在听众下方

expListW.get().setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Object e = (Object)adapterW.get().getChild(groupPosition, childPosition);
        int nmbr_childs = adapter.getChildrenCount(groupPosition);
        final int group_position = groupPosition;

        switch (childPosition) {
        case 0:
            if (nmbr_childs > 1 ) {
                myVoice = soundPool.load(PhraseActivity.wr.get(), sound[group_position][0], 2);
                soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                      @Override
                      public void onLoadComplete(SoundPool soundPool, int sampleId,
                          int status) {
                            soundPool.play(myVoice, 20, 20, 1, 0, 1f);
                      } 
                });
                speakerImage = (ImageView) v.findViewById(R.id.single);
                speakerImage.setBackgroundResource(R.drawable.smile);
            }
            else {
                myVoice = soundPool.load(PhraseActivity.this, sound[group_position][0], 2);
                soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                      @Override
                      public void onLoadComplete(SoundPool soundPool, int sampleId,
                          int status) {
                            soundPool.play(myVoice, 20, 20, 1, 0, 1f);
                      } 
                });
                speakerImage = (ImageView) v.findViewById(R.id.single);
                speakerImage.setBackgroundResource(R.drawable.smile);
            }   
            break;
        case 1:
            myVoice = soundPool.load(PhraseActivity.this, sound[group_position][1], 2);
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                  @Override
                  public void onLoadComplete(SoundPool soundPool, int sampleId,
                      int status) {
                        soundPool.play(myVoice, 20, 20, 1, 0, 1f);
                  } 
            });
            speakerImage = (ImageView) v.findViewById(R.id.single);
            speakerImage.setBackgroundResource(R.drawable.smile);
        break;              
        }
            return true;
        }
    });  

这是getChildView方法......

      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      final ViewHolder holder;
      if (convertView == null) {
          convertView = inflater.inflate(R.layout.activity_subphrase, parent, false);
          holder = new ViewHolder();
          holder.text = (TextView) convertView.findViewById(R.id.label_single);
          holder.text2 = (TextView) convertView.findViewById(R.id.label_couple);
          holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
          holder.imageView2 = (ImageView) convertView.findViewById(R.id.couple);
          convertView.setTag(holder);

      } else {  
          holder = (ViewHolder) convertView.getTag();
      }
      final int nChildren = getChildrenCount(groupPosition);

      final View v = convertView;
      switch (nChildren) {
        case 1:
            holder.imageView.setImageResource(0);
            holder.imageView2.setBackgroundResource(0);
            holder.text.setText(null);
            holder.text2.setText(contents[groupPosition][childPosition]);
            holder.imageView2.setBackgroundResource(R.drawable.man_woman_3);
            extra(groupPosition, category, holder.text, convertView, parent); 


            ViewTreeObserver vto = convertView.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            public void onGlobalLayout() {
                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);  //vet inte om denna metod är nödvändig
                holder.imageView2.setBackgroundResource(R.drawable.animation3);
                frameAnimation = (AnimationDrawable) holder.imageView2.getBackground();
                frameAnimation.start();   
            }});
            break;

        case 2:
            holder.imageView.setImageResource(0);
            holder.imageView2.setBackgroundResource(0);
            holder.text2.setText(null);
            holder.imageView.invalidate();
            holder.text.setText(contents[groupPosition][childPosition]);
            switch (childPosition) { // Switch-villkor för man eller kvinna. 
                case 0: // Man.
                    holder.imageView.setImageResource(R.drawable.man_33);
                    break;
                case 1: // Kvinna.
                    holder.imageView.setImageResource(R.drawable.woman_33);
                    break;
            }
            break;
      }
      notifyDataSetChanged();
      return convertView;
  }

非常感谢帮助!!!!!

1 个答案:

答案 0 :(得分:0)

我想我已经解决了我自己的问题。只需为实际的imageView调用bringToFront()。例如,如果imageHolder保持imageView:

holder.imageView.bringToFront()。