2012-12-08 53 views
0

我在我的活动中有可扩展的列表视图。我想为孩子们展示一个contextmenu。基本上我知道如何做到这一点,但是在实施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(); 

的祝酒会为每个组而不是为了孩子...

+0

我不认为我看不到任何东西在这里,会造成麻烦。没有'setFocusableInTouchMode'调用可能值得尝试。另外,我不认为xml部分是相关的。你可以显示你创建的位置并设置ExpandableListView吗?你设置了一个长的点击处理程序? – dokkaebi

+0

我只是注册上下文菜单。我之前没有使用setFocusableInTouchMode尝试它,这没有任何不同。我调整了上面的代码片段。 –

+0

我刚刚将您的代码添加到其中一个ExpandableListViews,并且它工作正常。我为两个小组和儿童都敬酒,否则他们的行为会正常。它可能与你的列表的选择模式有关 - 我必须将我的调用删除到'listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);'。否则,也许它与sherlock有关?也许sherlock自动设置一个上下文动作栏或其他东西? – dokkaebi

回答

1

我终于弄明白了......它不能在xml中设置touchable = false。在组件和子视图的适配器的getView-methods中调用setTouchable(false)后,所有事情都按照假定的方式工作。

+0

我面临同样的问题。我无法找到setTouchable方法。您能否详细说明您的解决方案? –

0

我面临同样的问题。上面的解决方案并没有为我工作。我找到了一个简单的解决方案!

这是帮助我:

在我的方法ExpandableListAdapter:

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