0

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

设置可展开的列表视图:

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

祝酒会为每个小组展示,但不是为孩子们...

4

2 回答 2

1

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

于 2012-12-09T15:18:41.190 回答
0

我遇到了同样的问题。上述解决方案对我不起作用。我找到了一个简单的解决方案!

这对我有帮助:

在我的 ExpandableListAdapter 方法中:

public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View concertView, ViewGroup parent) {
    ... 
    concertView.setLongClickable( true);
于 2014-10-23T15:54:45.243 回答