【问题标题】:ImageView in getChildView not anymore updating after calling OnChildClickListener调用 OnChildClickListener 后 getChildView 中的 ImageView 不再更新
【发布时间】:2013-07-03 12:06:01
【问题描述】:

现在我遇到了一个关于可扩展列表视图的问题。问题是onChildClickListener 和getChildView 之间似乎存在冲突。

简而言之:当单击一个分组项时,会弹出两个子菜单项。在这些 (2) 子菜单中,有一个加载图像的图像视图。如果单击其中一个项目,则会生成另一个图像视图 - 这次来自 onChildClickListener 方法。到目前为止,一切都很好。但是 - 如果继续在应用程序中展开另一个分组项 - 从 onChildClickListener 方法生成的图像视图仍然存在(不是来自 getChildView 的图像视图)。

我在堆栈上发现了一个类似的问题,您可以在此处找到。但问题出在侦听器和 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;
  }

非常感谢帮助!!!!!!

【问题讨论】:

    标签: android android-listview android-imageview expandablelistview


    【解决方案1】:

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

    holder.imageView.bringToFront().

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多