我的活动中有一个可扩展的列表视图。我想为孩子们展示一个上下文菜单。基本上我知道如何做到这一点,但是在实现上下文菜单时,它永远不会出现在子视图中。只是长时间点击组视图,我没有找到这种行为的原因。有什么想法吗?提前致谢!
设置可展开的列表视图:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View v = inflater.inflate(R.layout.listed_data, container, false);
parser = new SvwXmlparser("a", getSherlockActivity());
elv = (ExpandableListView) v.findViewById(R.id.svwexplist);
return v;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
registerForContextMenu(elv);
infos = parser.parse();
showData();
}
public void showData()
{
adapter = new SvwInfoAdapter(getSherlockActivity(), infos);
elv.setAdapter(adapter);
}
适配器:
public Object getChild(int groupPos, int childPos) {
return children.get(groupPos).get(childPos);
}
public long getChildId(int groupPos, int childPos) {
return childPos;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
//TODO:modify
String player = (String)getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.svw_team_child_layout, null);
}
convertView.setFocusableInTouchMode(true);
TextView players = (TextView) convertView.findViewById(R.id.tvteamChild);
/*if(childPosition> 1)
{
players.setText((childPosition-1) + ". " + player);
}
else
{
players.setText(player);
}*/
players.setText(player);
return convertView;
}
public int getChildrenCount(int groupPos) {
return children.get(groupPos).size();
}
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
创建上下文菜单:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
Toast toas =Toast.makeText(getSherlockActivity(), groupPos + " - " + childPos, Toast.LENGTH_LONG);
toas.show();
祝酒会为每个小组展示,但不是为孩子们...