【问题标题】:Multiple Textviews in ExpandableListView AndroidExpandableListView Android中的多个Textview
【发布时间】:2015-12-22 10:50:12
【问题描述】:

我目前有一个可以很好地填充的可扩展列表视图。但是,当子视图填充时,它会为每个条目创建单独的视图和行。我想在视图中有一个视图和多个文本视图,以及我将添加的一个按钮。数据是从 HASHMAP 填充的。我已经尝试了很多事情,但无法在列表视图中正确填充多个文本视图。 所有数据都来自这个 hashmap:

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

这是我的自定义适配器的获取子视图方法。

@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;
}

这是我现在想要做的事情的形象。

我需要在一行中突出显示数据,这样我就可以在没有一堆行的情况下添加按钮等。我真的无法弄清楚如何正确地做到这一点。

【问题讨论】:

  • 是否意味着您希望子行的多个视图中有多个数据?
  • 现在我创建了 3 行,每行都有一个 textview。我想在该行中有 3 个文本视图的 1 行。我尝试将文本视图添加到一行中,但它们没有正确添加,它们不包含预期的数据。
  • 那是什么问题?您可以相应地更改子行布局。
  • 我认为这是我的主要问题。我不完全理解如何将行更改为具有多个文本视图的一行。我不明白我会怎么做。

标签: java android listview hashmap expandablelistview


【解决方案1】:

我是个白痴。我想到了。谢谢@Krupal,您为我指明了正确的方向。如果您将任何内容作为答案,我会选择您,因为您让我以正确的方式思考。

所以我最终在这里创建了一个自定义列表适配器。我不会添加整个课程以保持简短。关键是使用设置适配器只返回一个孩子。然后在返回的孩子(下面的 XML)中,我添加了所有需要的字段。所以现在当我展开列表时,只有一个孩子可见。同样在我的 getChildView() 中,我为孩子的布局定义了所有需要的条目。不知道这是否是完美的方法,但可以很好地满足我的需求。

这是我用我的数据实际配置孩子的地方。

  @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!
}

将子项计数设置为 1

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

子元素的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多