在Android中创建包含不同元素的每个项目的列表

时间:2011-01-13 04:56:37

标签: android android-layout

我是Android开发的新手。我使用ListView之类的接口创建了一个Activity,但没有使用ListView,因为列表中的每个项目(行)可能有不同的元素。我已经通过在ScrollView中包含LinearLayouts成功创建了它,但XML看起来很混乱。我还为每个内部LinearLayouts添加了onClickListener。

有更好的方法吗?

感谢。

此致

dezull

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:layout_height="fill_parent"
  android:orientation="vertical" 
  android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
 <ScrollView 
  android:id="@+id/ScrollView01" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:layout_weight="1">
  <LinearLayout 
   android:id="@+id/LinearLayout02" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:orientation="vertical">
   <LinearLayout 
    android:id="@+id/wo_status_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:background="@color/desc_button"
    android:focusable="true"
    android:padding="3dp">
    <TextView
     android:id="@+id/wo_status"
     android:text="@string/wo_status"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:id="@+id/wo_status"
     android:text="Waiting for Parts"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
   </LinearLayout>
   <LinearLayout 
    android:id="@+id/wo_prob_desc_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:background="@color/desc_button"
    android:focusable="true"
    android:padding="3dp">
    <TextView
     android:id="@+id/wo_prob_desc"
     android:text="@string/wo_status"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:id="@+id/wo_prob_desc"
     android:text="New problem"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
   </LinearLayout>
  </LinearLayout>
 </ScrollView>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

我是Ledgerist的首席开发人员。我们所做的是创建一个Adapter的子类,它按照上面的建议实现了getView。

在getView方法中,您将创建一个ViewHolder对象,因为我确信它会在Web上的许多教程中显示。您将要创建可能要在ViewHolder中显示的每个控件的实例。当您开始实现单元格时,您需要使用setVisibility方法将您不想显示的所有控件设置为GONE。

希望这有帮助,

James K.,首席开发人员 www.vervv.com | support@vervv.com

在Twitter上关注我们:@vervv http://twitter.com/vervv

答案 1 :(得分:0)

你可以继承Adapter&amp;实现getView(); 适配器允许您使用多个View,并且您可以setOnClickListener用于任何;  与孩子一起回收linearLayouts; android已经有各种用途的适配器子类; ArrayAdapter,SimpleAdapter,CursorAdapter,.. etc; 然后是ListView.setAdapter(yourAdapter);

另一个技巧是使用带有scrollView的适配器; 将LinearLayout设置为scrollView子项,然后设置为linearLayout.addview(adapter.getView(...));

希望你有所帮助;

相关问题