代码之家  ›  专栏  ›  技术社区  ›  user155542

如何在android中的expandablelistview中获取组的视图?

  •  5
  • user155542  · 技术社区  · 13 年前

    我是黑人,所以,我的英语不好。

    所以…我需要在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;
    }
    

    知道吗?谢谢

    4 回复  |  直到 13 年前
        1
  •  14
  •   kalabalik    8 年前

    要从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);
    }
    
        2
  •  1
  •   Kasra Rahjerdi    13 年前

    如果您的代码是正确的,那么您想获取子级的组视图,以便对其调用getTag(),对吗?

    如果是这样,为什么不跳过这一步,访问由 programas.get(groupPosition) 手动?

    您可以拨打以下电话:

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

    编辑以回应您的评论:

    这里的问题是,您将无法通过适配器获取组的视图,因为由于列表中的视图被回收,这可能需要重新创建视图。如果这个方法不起作用,我强烈建议修改您的代码使其起作用。

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

        3
  •  1
  •   Kevin van Zyl    9 年前

    我尝试了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);
    

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

        4
  •  -2
  •   Nick    11 年前

    我使用以下代码访问了一个组中的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;
    

    }

    我希望它能有所帮助