如何在 Android ListView 中添加子项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20320794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:38:08  来源:igfitidea点击:

How to add a sub item in Android ListView?

androidlistview

提问by CrazyLearner

I have made a android listView taking the help from Vogella.comusing following layout and ListActivity class.

我使用以下布局和 ListActivity 类在Vogella.com的帮助下制作了一个 android listView 。

RowLayout.xml

行布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/icon" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout> 

MyListActivity.java

MyListActivity.java

package de.vogella.android.listactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListActivity extends ListActivity {
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };
    // use your own layout
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.rowlayout, R.id.label, values);
    setListAdapter(adapter);
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
  }
}

I want to add a sub item below the textView and keep the full text portion in the center of each row. How can I do it?

我想在 textView 下方添加一个子项,并将全文部分保留在每行的中心。我该怎么做?

采纳答案by GrIsHu

A ListViewitem can have it's own custom layout. When you create your adapter for the ListViewyou can pass in the layout id to the Adapter constructor. See SimpleAdapterand ArrayAdapter.

一个ListView项目可以有它自己的自定义布局。当您为您创建适配器时,ListView您可以将布局 ID 传递给 Adapter 构造函数。请参阅SimpleAdapterArrayAdapter

If you want to show some more details like image and text or two textview then You will have to extend an Adapter and implement getView()to property set the image+text.

如果你想显示更多的细节,比如图像和文本或两个文本视图,那么你将不得不扩展一个适配器并实现getView()属性设置图像+文本。

Check out Custom ListView

查看自定义列表视图

And if you want to categorize the ListView in sections then you should go for the Section ListView in Androidalso check Section Header in ListView

如果你想在部分中对 ListView 进行分类,那么你应该去Android 中部分 ListView也检查ListView 中的部分标题

回答by Thein

You can use the built-in android.R.layout.simple_list_item_2 to create two line textView.

您可以使用内置的 android.R.layout.simple_list_item_2 创建两行 textView。

enter image description here

在此处输入图片说明

回答by Looking Forward

For that you have to create custom list view. Here is the Link...

为此,您必须创建自定义列表视图。这是链接...

回答by Jigar Pandya

In order to add sub items to your items in List View, Use Expandable List View.

要向列表视图中的项目添加子项目,请使用可扩展列表视图。

Have a look at thisexample.

看看这个例子。

You can ask if you have any further queries :)

你可以问你是否有任何进一步的问题:)