现在我遇到了一个关于可扩展列表视图的问题。问题是onChildClickListener 和getChildView 之间似乎存在冲突。
简而言之:当单击一个组项时,会弹出两个子菜单项。在这些 (2) 子菜单中,有一个加载图像的图像视图。如果单击其中一个项目,则会生成另一个图像视图 - 这次来自 onChildClickListener 方法。到目前为止,一切都很好。但是 - 如果继续扩展应用程序中的另一个组项 - 从 onChildClickListener 方法生成的图像视图仍然存在(不是来自 getChildView 的图像视图)。
我在堆栈上发现了一个类似的问题,您可以在此处找到。但是问题出在侦听器和 getGroupView 之间。
自定义视图未使用 ExpandableListView 上的 OnGroupClickListener 更新
这是什么原因?跟缓存有关系吗?任何建议如何解决所有这些问题?我尝试了几种方法……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;
}
非常感谢帮助!!!!!!