在Android中包含不同元素的每个项目创建一个列表
我是Android开发新手。我使用类似ListView的界面创建了一个Activity,但没有使用ListView,因为列表中的每个项目(行)都可能具有不同的元素。我通过在ScrollView中包含LinearLayouts成功创建了它,但是XML看起来很混乱。我还将onClickListener添加到每个LinearLayout内部。在Android中包含不同元素的每个项目创建一个列表
有没有更好的方法来做到这一点?
谢谢。
问候,
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>
我是Ledgerist的首席开发人员。我们所做的是创建一个适配器的子类,它实现了上面建议的getView。
在getView方法中,您将创建一个ViewHolder对象,因为我确定在整个Web上的许多教程中都会显示该对象。您需要创建可能要在ViewHolder中显示的每个控件的实例。当你开始实现单元格时,你需要使用setVisibility方法来设置你不想显示的所有控件。
希望这是有帮助的,
詹姆斯·K,首席开发 www.vervv.com | [email protected]
按照我们在Twitter:@vervv http://twitter.com/vervv
你也可以继承适配器&实现getView(); 适配器允许您使用多个视图,并且您可以为任何setOnClickListener; 用你喜欢的小孩重新调整linearLayouts; android已经有各种用途的适配器子类; ArrayAdapter,SimpleAdapter,CursorAdapter,..etc; 然后ListView.setAdapter(yourAdapter);
另一个窍门是使用适配器与scrollView; 将LinearLayout设置为scrollView子然后linearLayout.addview(adapter.getView(...));
希望你帮忙;
我已经看到了使用适配器的例子,但允许每个元素是唯一的? (例如:一个有按钮,另一个有图像)。更具体地说,我想创建一个像Android的偏好的活动,但是作为一种形式(如在Android的Marketplace上的Ledgerist应用程序中)。 – dezull 2011-01-13 07:36:40
谢谢,从未想过Ledgerist的首席开发人员会回答我的问题:-)。我喜欢使用setVisibility的想法,我认为它比为每个单元格使用单独的布局更好。 – dezull 2011-01-20 09:57:05