简体   繁体   English

如何在Android中制作商品的列表视图?

[英]How to make List view for items in Android?

Please help me out here. 请帮我在这里。 I need to make a list view as shown in the sample pic for my android app . 我需要制作一个列表视图 ,如我的android应用程序的示例图片所示。 How to do it? 怎么做? Also I need to add image along it as seen in the sample pic. 我还需要添加图像 ,如示例图片所示。 and link it to another view to show some more details.please check links shown below. 并将其链接到其他视图以显示更多详细信息。请检查下面显示的链接。

替代文字

pic1 PIC1

First create a layout for your list item in layout/list_item.xml (or any name). 首先在layout / list_item.xml(或任何名称)中为列表项创建一个布局。 I've shown only text field for the item, but you can change the view below to include both image and a label. 我只显示了该项目的文本字段,但是您可以更改下面的视图以同时包含图像和标签。

  <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>

Define a listView in another layout eg layout/list.xml 在另一个布局中定义一个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>

In your activity get the handle to list view and add data to it. 在您的活动中,获取列表视图的句柄并向其中添加数据。

 // 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);

You need to implement a BaseAdapter . 您需要实现BaseAdapter BaseAdapter defines a getView method, which will return the view that will be used by the list items. BaseAdapter定义了一个getView方法,该方法将返回列表项将使用的视图。 You can create the view diagrammatically or load it from XML. 您可以以图表方式创建视图或从XML加载视图。 Then in your ListActivity you use the setListAdapter method. 然后,在ListActivity中,使用setListAdapter方法。

See an example here . 在这里查看示例。

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

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