不是过于优雅的解决方案,但它帮助了我:
我保留了原来的 groupIndicator。我喜欢这种行为。
在 groupItem 布局上,我只是用空视图挡住了空间,这样原来的 groupIndicator 仍然可以点击:
<LinearLayout>
....
<!--just dummy space underneath the default expand_collapse group icon - this way original expand collapse behavior is preserved on the icon-->
<View
android:layout_width="25dp"
android:layout_height="match_parent">
</View>
<!-- text view will have actionListener -->
<TextView
android:id="@+id/category_name"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
.... />
....
</LinearLayout>
比在创建 ExpandableListAdapter 时潜入 callBack 对象并将其钩住单击“category_name”(对于子元素和组元素)
public class ExpandableListAdapter extends BaseExpandableListAdapter {
public interface OnCategoryActionListener {
boolean onCategorySelected(long categoryId);
}
....
public ExpandableListAdapter(Activity context, Category rootCategory,
OnCategoryActionListener callBack) {
this.context = context;
this.rootCategory = rootCategory;
this.callBack = callBack;
}
....
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View
convertView, ViewGroup parent) {
String categoryName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category, null);
}
TextView item = (TextView) convertView.findViewById(R.id.category_name);
item.setTypeface(null, Typeface.BOLD);
item.setText(categoryName);
item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
callBack.onCategorySelected(getGroupId(groupPosition));
}
});
.....
您现在要做的就是在主片段类中正确初始化 ExpandableListAdapter
expListAdapter = new ExpandableListAdapter(getActivity(), this.rootCategory,
new OnCategoryActionListener());
expListView = (ExpandableListView) getActivity().findViewById(R.id.categories_list);
expListView.setAdapter(expListAdapter);