【发布时间】:2014-05-31 14:19:11
【问题描述】:
我有一个列表 (ExpandableListView),列表中的每个项目都有一个 ToggleButton,它在切换时显示/隐藏子项目(孩子总是 1,它是一种带有两个按钮的工具栏)。感谢this 教程,我可以设置一个自定义按钮而不是expandablelistview 的指示器,并且我做到了,这样除了在单击列表项时显示工具栏之外,我还可以做其他事情。另外,我使用this question 的答案在打开另一个工具栏时自动关闭打开的工具栏。 因此,当触摸屏幕上不是其 ToggleButton 的任何内容时,我需要折叠当前展开的工具栏(即使单击工具栏的按钮之一,只要发送它自己的“onClick”,我也需要折叠它) .
这是应用程序的图片:
这是 ExpandableListView:
<ExpandableListView
android:id="@+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="@drawable/toggle_button_selector" >
</ExpandableListView>
这是组项的 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="@+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:contentDescription="@string/icondescription" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="@+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="@+id/img"
android:layout_below="@+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ToggleButton
android:id="@+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/text"
android:layout_marginRight="15dp"
android:background="@drawable/toggle_button_selector"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="@android:color/transparent"
android:textOff=""
android:textOn="" />
子项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="@drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="@drawable/toolbar_selector"
android:gravity="center"
android:text="@string/details" />
<TextView
android:id="@+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="@drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="@drawable/toolbar_selector"
android:gravity="center"
android:text="@string/send" />
</LinearLayout>
适配器的相关部分:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
private final Integer[] imageId;
private int lastExpandedGroupPosition;
public CustomList(Activity context, List<Item> names, Integer[] imageId) {
this.context = context;
this.names = names;
this.imageId = imageId;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_single, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.text);
TextView txtData = (TextView) convertView.findViewById(R.id.counter);
ImageView imageView = (ImageView) convertView.findViewById(R.id.img);
ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
final ExpandableListView list = (ExpandableListView) parent.findViewById(R.id.normalList);
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
}
});
} else if(/*other conditions*/) {
imageView.setImageResource(imageId[2]);
toggle.setVisibility(View.GONE);
} else {
imageView.setImageResource(imageId[1]);
toggle.setVisibility(View.GONE);
}
return convertView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
}
TextView details = (TextView) convertView.findViewById(R.id.details);
TextView send = (TextView) convertView.findViewById(R.id.send);
details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something
}
});
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something else
}
});
return convertView;
}
@Override
public void onGroupExpanded(int groupPosition) {
ToggleButton button = (ToggleButton) ((ExpandableListView) context
.findViewById(R.id.normalList)).getChildAt(lastExpandedGroupPosition)
.findViewById(R.id.toggle);
if(groupPosition != lastExpandedGroupPosition && button.isChecked()) {
button.performClick();
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
}
最后是活动中的重要部分:
lv = (ExpandableListView)findViewById(R.id.normalList);
lv.setGroupIndicator(null);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
//do some things
return true; //this tells the list that it mustn't show the child items
}
});
adapter = new CustomList(myActivity.this, list, icons);
lv.setAdapter(adapter);
P.S.:隐藏打开工具栏的代码不能正常工作,但我真的不明白为什么。但这是另一个故事......
--- 编辑 ---
关于 P.S.上图:now I know why that happens.
【问题讨论】:
标签: android expandablelistview togglebutton