我正在使用expandablelistview。无论如何,单击内部图像后,我是否可以展开组行?我知道要让图像响应点击,我必须设置其焦点。现在,一旦该图像响应了单击(在我的客户适配器内部),我如何以编程方式扩展/折叠它所属的特定组行?
谢谢
最佳答案
在适配器中
private OnItemSelectedListener onItemSelectedCallback;
public interface OnItemsSelectedListener {
public void onImageSelected(int groupPos);
}
public YourAdapter(Context context) {
try {
this.onItemSelectedCallback = (OnItemSelectedListener ) context;
}
catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnItemSelectedListener ");
}
}
在getView()中添加它
ImageView imageView = new ImageView();
imageView.setTag(R.id.tagGroupPosition, groupPosition);
imageView.setOnClickListener(onClickListener);
在OnClickListener中添加
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int groupPos = ((ImageView) v).getTagId(R.id.tagGroupPosition);
onItemSelectedCallback.onImageSelected(groupPos);
}
}
然后在 Activity 中,您必须实现YourAdapter.OnItemsSelectedListener重写onImageSelected
@Override
public void onImageSelected(int groupPos){
if(expandableList.isGroupExpanded(groupPos)){
expandableList.collapseGroup(groupPos);
}else{
expandableList.expandGroup(groupPos);
}
}