如何在android中的expandablelistview中查看组?

问题描述 投票:5回答:4

即时通讯,我的英语不好。

所以..我需要在expandablelistview中查看组的视图,以通过view.getTag()方法获取对象标记。

在这个例子中关注我:

 ExpandableListView

    --> group (i need this view)
    ----> child
    ----> child
    ----> child

    --> group (i need this view)
    ----> child
    ----> child

我的代码:

    @Override
public boolean onChildClick(final ExpandableListView parent, final View v,
        final int groupPosition, final int childPosition, final long id) {

        /* I NEED GET VIEW OF GROUP FOR GET YOUR TAG*/

        View vParent = parent.getChildAt(groupPosition); // dont work after first group 
        Programa v2 = (Programa) parent.getTag(); // return null

        // v parameter is a child of group

    return true;
}

在我的适配器中:

    @Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    TwoLineListItem view = (TwoLineListItem) LayoutInflater.from(contexto)
            .inflate(android.R.layout.simple_expandable_list_item_2,
                    parent, false);

    String programa = map.keySet().toArray(new String[map.keySet().size()])[groupPosition];

    view.getText1().setText(programa);
    view.getText2().setText("PROGRAMA LOCAL");

    view.setTag(programas.get(groupPosition)); // i need get this in child click listener

    return convertView = view;
}

任何的想法?谢谢

android expandablelistview
4个回答
1
投票

如果你的代码是正确的,你想获得孩子的组视图,以便你可以调用它的getTag(),对吗?

如果是这样,为什么不跳过该步骤并访问由programas.get(groupPosition)手动设置的标记值?

你可以通过调用:

programas.get(groupPosition)就在你的onChildClick方法的顶部,因为你也得到了组位置值。

编辑以回应您的评论:

这里的问题是,您无法通过适配器获取组的视图,因为它可能涉及由于列表中的视图的回收而重新创建视图。如果此方法不起作用,我强烈建议您修改代码以使其正常工作。

如果programas是适配器的输入之一,则在适配器上调用getGroup(groupPosition)以访问它。否则,在适配器类中创建一个公共方法以允许检索该值。


13
投票

要从ExpandableListView获取组视图,请执行以下操作:

public View getGroupView(ExpandableListView listView, int groupPosition) {
  long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition);
  int flatPosition = listView.getFlatListPosition(packedPosition);
  int first = listView.getFirstVisiblePosition();
  return listView.getChildAt(flatPosition - first);
}

1
投票

我尝试了AedonEtLIRA答案,但它只获得了列表中的第一项。

我的解决方案是在我的适配器的getGroupView方法中设置一个标记。

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.listitem_group, null);
    }

    //This is your data object that you might be using for storage that can be returned by your getGroup(groupPosition) function
    GroupDataDto groupDataDto = (GroupDataDto) getGroup(groupPosition);
    String name = groupDataDto.getName();
    convertView.setTag(name);
...

现在在您的片段/活动中,在您的onGroupExpand和onGroupCollapse中:

    GroupDataDto groupDataDto = (GroupDataDto) listAdapter.getGroup(groupPosition);
    ImageView arrow = (ImageView) getView().findViewWithTag(groupDataDto.getName()).findViewById(R.id.expandable_arrow_down);

然后我可以在箭头上做动画,这就是为什么我想要获得该组视图。


-2
投票

我使用以下代码访问组中的TextView:

@Override
public boolean onChildClick(final ExpandableListView parent, final View v,
    final int groupPosition, final int childPosition, final long id) {

    View vParent = mAdapter.getGroupView(groupPosition, true, null, null); 
    Programa v2 = (Programa) vParent.getTag(); 

return true;

}

我希望它有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.