因此我的ExpandableListView
具有定义为:
group_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/GroupName"
style="@style/ListViewRowStyle"
android:paddingLeft="40dp"
android:textSize="18sp" >
</TextView>
<ImageView
android:id="@+id/Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:contentDescription="@string/default_content_description_text"
android:src="@drawable/ic_menu_moreoverflow_normal_holo_light" >
</ImageView>
</RelativeLayout>
单击TextView
时,它将展开或折叠,具体取决于当前是否显示子行。我已将OnClickListener
附加到组行中的ImageView
。单击此ImageView
后,我会启动PopupMenu
,如下图所示:
显示PopupMenu
并单击其中一个操作后,我想对该组的所有子项执行操作。问题是我无法确定点击ImageView
的行。
我想出如何将动作应用于所有孩子的唯一方法是使用ContextMenu
,如下图所示:
我想避免使用ContextMenu
,因为对于用户来说,对组行进行LongClick可能并不明显,这会导致它会对子行执行一些操作。我认为更明显的设计是将PopupMenu
锚定到ImageView
(在我的情况下是一个菜单图标)并将操作应用于该组的子行。如何使用PopupMenu
获得此功能?
答案 0 :(得分:4)
答案 1 :(得分:4)
你需要:
View
对PopUpMenu(您的ImageView
)PopUpMenu
,在本例中为popup_select_deselect.xml
onMenuItemClickListener
被声明为内部类,在本例中为onMenuItemClickListener_View
代码:
//TODO initialize rows[]
for (int i = 0; i < rows.lenght; i++){
//inflate you group_row
getLayoutInflater().inflate(R.layout.group_row, (ViewGroup)findViewById(R.id.rows_container));
ImageView v_Overflow = (ImageView)findViewById(R.id.Menu);
//Set onClickListener
v_Overflow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View v_Button = v;
PopupMenu pum= new PopupMenu(YourActivity.this, v);
//set my own listener giving the View that activates the event onClick (i.e. YOUR ImageView)
pum.setOnMenuItemClickListener(new onMenuItemClickListener_View(v) );
//inflate your PopUpMenu
getMenuInflater().inflate(R.menu.popup_select_deselect, pum.getMenu());
pum.show();
}
});
//Update the id of your TextView
.setId(i); //the i value will be your UNIQUE id for the ImageView
}
上面的代码只是您自己的OnMenuItemClickListener
将做的静态声明。
注意以下侦听器的构造函数中的给定View
。创建此侦听器的实例时,View
Id与XML布局中声明的相同。在运行时它将被更新,因此当调用方法onMenuItemClick
时,TextView
ID已经更改。
以下是代码:
private class onMenuItemClickListener_View implements OnMenuItemClickListener{
View v_View;
public onMenuItemClickListener_View(View v){
v_View=v;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int i = v_View.getId();
switch (item.getItemId()) {
case R.id.popupItemSelectAll:
Toast.makeText(YourActivity.this, "Popup Select All for View #: " + rows[i], Toast.LENGTH_SHORT).show();
//TODO your code to select all
return true;
case R.id.popupItemDeselectAll:
Toast.makeText(YourActivity.this, "Popup Deselect All for View #: " + rows[i], Toast.LENGTH_SHORT).show();
//TODO your code to deselect all
return true;
default:
return false;
}
}
}
}