現在,我遇到了有關expandablelistview的問題。問題是onChildClickListener和getChildView之間似乎存在衝突。調用OnChildClickListener之後,getChildView中的ImageView不再更新
簡而言之:當點擊一個組合項時,彈出兩個子菜單項。在這些(2)子菜單中有一個加載圖像的imageview。如果其中一個項目被點擊,則會生成另一個imageview - 這次是從onChildClickListener方法。到現在爲止還挺好。但是 - 如果繼續在應用中擴展另一個groupitem - 從onChildClickListener方法生成的imageview仍然存在(而不是getChildView的imageview)。
我在堆棧上發現了一個類似的問題,你可以在這裏找到。但問題出在監聽器和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;
}
非常感激救命!!!!!