没有为可扩展列表视图的子项显示上下文菜单

时间:2012-12-08 22:23:15

标签: android contextmenu expandablelistview

我的活动中有一个可扩展的列表视图。我想为孩子们展示一个上下文菜单。基本上我知道如何做到这一点,但是在实现contextmenu时,它永远不会出现在子视图中。只是长时间点击组视图,我没有找到这种行为的原因。也许有什么想法? 提前谢谢!

设置可展开列表视图:

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();

每个小组都会举杯祝酒,而不是给孩子们......

2 个答案:

答案 0 :(得分:1)

我终于明白了......在xml中设置touchable = false是行不通的。在适配器的getView-methods中调用setTouchable(false)进行组视图和子视图后,一切都按预期工作。

答案 1 :(得分:0)

我遇到了同样的问题。上面的解决方案对我没有用。我找到了一个简单的解决方案!

这对我有所帮助:

在方法的ExpandableListAdapter中:

public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View concertView, ViewGroup parent) {
    ... 
    concertView.setLongClickable( true);