简体   繁体   English

Android ExpandableListView中的多个Textviews

[英]Multiple Textviews in ExpandableListView Android

I currently have an expandable list view that is populating just fine. 我目前有一个可填充的列表视图,可以很好地填充它。 However when the child view is filling it creates individuals views and rows for each entry. 但是,在填充子视图时,它将为每个条目创建单独的视图和行。 I would like to have one view and multiple textviews in the view as well as a button that I will be adding. 我想在该视图中有一个视图和多个textview,以及一个要添加的按钮。 The data is being populated from a HASHMAP. 正在从HASHMAP填充数据。 I have tried many things and cannot get multiple textviews to populate correctly in the listview. 我已经尝试了很多事情,但无法获得多个textview才能正确地填充到listview中。 All data comes from this hashmap: 所有数据均来自此哈希图:

 private HashMap<String, List<String>> _deviceDetails;

This is the get child view method of my custom adapter. 这是我的自定义适配器的get child view方法。

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);
    return convertView;
}

This is an image of what I have now with what I wish to do. 这是我现在想要做的事情的图像。 在此处输入图片说明

I need the data highlighted inside of one row so that I can add buttons etc without a bunch of rows. 我需要在一行中突出显示数据,以便我可以添加按钮等而无需一堆行。 I genuinely cannot figure out how to do this properly. 我确实无法弄清楚如何正确执行此操作。

I'm an idiot. 我是个白痴。 I figured it out. 我想到了。 Thank you @Krupal you pointed me in the right direction. 谢谢@Krupal,您指出了我正确的方向。 If you place anything as an answer i'll select you because you got me thinking the right way. 如果您有任何答案,我会选择您,因为您让我思考正确的方法。

So what I ended up doing here was I created a custom List Adapter. 因此,我最终在这里所做的是创建了一个自定义列表适配器。 I won't add the entire class to keep it short. 我不会增加整个课程的内容。 The key was to use set the adapter to only return one child. 关键是使用将适配器设置为仅返回一个孩子。 Then in the child that was returned (XML below) I had all of my needed fields added in. So now when I expanded the list only one child would be visible. 然后,在返回的子项(下面的XML)中,我添加了所有需要的字段。因此,当我扩展列表时,只有一个子项可见。 Also in my getChildView() I defined all needed entries for the layout for the children. 同样在我的getChildView()中,我定义了子布局所需的所有条目。 Don't know if this is the perfect way to do it but worked nicely for my needs. 不知道这是否是完美的方法,但是可以很好地满足我的需求。

This is where I actually configured the child with my data. 这是我实际为孩子配置数据的地方。

  @Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, 0);
    final String childTextSecond = (String) getChild(groupPosition, 1);
    final String childTextThird = (String) getChild(groupPosition, 2);
    final String deviceName = (String) getChild(groupPosition, 3);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    TextView txtListChildTwo = (TextView) convertView
            .findViewById(R.id.lblListItemTwo);

    TextView txtListChildThree = (TextView) convertView
            .findViewById(R.id.lblListItemThree);

    //rest of logic!
}

Setting the children count to 1 将孩子数设置为1

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

XML Layout for the child elements 子元素的XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

<RelativeLayout
    android:layout_width="340dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|right"
    android:background="#f9fcff">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:background="#4d86ff">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:layout_marginLeft="10dp"
                android:text="Large Text"
                android:id="@+id/lblListItem"
                android:textColor="#ffffff"
                android:layout_alignTop="@+id/textView5"
                android:layout_toRightOf="@+id/textView5"
                android:layout_toEndOf="@+id/textView5" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:text="Device ID: "
                android:id="@+id/textView5"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textColor="#ffffff" />

            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="Tags"
                android:id="@+id/btnTags"
                android:background="#ffffff"
                android:singleLine="true"
                android:layout_alignTop="@+id/lblListItem"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:text="Status: "
                android:id="@+id/textView6"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textColor="#ffffff" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:text="Large Text"
                android:id="@+id/lblListItemTwo"
                android:textColor="#ffffff"
                android:layout_alignTop="@+id/textView6"
                android:layout_toRightOf="@+id/textView6"
                android:layout_toEndOf="@+id/textView6" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginBottom="10dp"
                android:textColor="#ffffff"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:text="Description: "
                android:id="@+id/textView7"
                android:layout_marginStart="15dp"
                android:layout_alignTop="@+id/lblListItemThree"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="6dp"
                android:layout_marginRight="13dp"
                android:layout_marginBottom="10dp"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:text="Large Text"
                android:id="@+id/lblListItemThree"
                android:textColor="#ffffff"
                android:layout_marginStart="39dp"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/textView7"
                android:layout_toEndOf="@+id/textView7" />

        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM