[英]How to make List view for items in Android?
請幫我在這里。 我需要制作一個列表視圖 ,如我的android應用程序的示例圖片所示。 怎么做? 我還需要添加圖像 ,如示例圖片所示。 並將其鏈接到其他視圖以顯示更多詳細信息。請檢查下面顯示的鏈接。
首先在layout / list_item.xml(或任何名稱)中為列表項創建一個布局。 我只顯示了該項目的文本字段,但是您可以更改下面的視圖以同時包含圖像和標簽。
<TextView xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:textSize="14dp"
a:paddingBottom="5dp"
a:paddingTop="5dp">
</TextView>
在另一個布局中定義一個listView,例如layout / list.xml
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:orientation="vertical"
a:stretchColumns="1">
<ListView a:id="@+id/paramList" a:layout_width="fill_parent"
a:layout_height="fill_parent" />
</LinearLayout>
在您的活動中,獲取列表視圖的句柄並向其中添加數據。
// Find the list view component in your layout.
ListView list = (ListView) findViewById(R.id.paramList);
// obtain data
List data = ... //some list data
// use a list adapter to render your list item in your list view!!
// The item is rendered using the list_item.xml layout.
// Here you can have any layout, with image and text as in your pic.
ListAdapter adapter = new ArrayAdapter(this, R.layout.list_item, data);
list.setAdapter(adapter);
您需要實現BaseAdapter 。 BaseAdapter定義了一個getView方法,該方法將返回列表項將使用的視圖。 您可以以圖表方式創建視圖或從XML加載視圖。 然后,在ListActivity中,使用setListAdapter方法。
在這里查看示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.