2012-08-08 82 views
0

我想创建以下格式的自定义列表视图。例如如何在自定义列表视图中添加textview和editview?

_______________________________________ 
|          | 
| -----------   ------------ | 
| Principal    EditView  | 
| -----------   ------------ | 
|          | 
| -----------   ------------ | 
| Years     EditView  | 
| -----------   ------------ | 
|          | 
| -----------   ------------ | 
| Rate of Int    EditView  | 
| -----------   ------------ | 
|_______________________________________| 

_______________________________________ 
|          | 
| -----------   ------------ | 
| Interest    EditView  | 
| -----------   ------------ | 
|_______________________________________| 

_______________________________________ 
|          | 
| -----------   ------------ | 
| Simple Int    EditView  | 
| -----------   ------------ | 
|_______________________________________| 

_______________________________________ 
|          | 
| ------------   ------------ | 
| Compond Int   EditView  | 
| ------------   ------------ | 
|_______________________________________| 

在短列表视图中的ITEM1应包含3文本和editviews和ITEM2应具有2文本和EditView中。这两项都应该得到不同的意见。

,因为我是新到Android,我能够创建简单的列表视图,但我不知道如何创建各种视图列表视图。

回答

1

你需要创建一个自定义的适配器,该适配器将能够夸大其自己的XML查看每个细胞。

新的XML视图应该简单有你的ListView会用最复杂的布局,对不为子视图使用信息的每一个细胞,仅仅是子视图设置为不可见。

至少应该引导你在正确的方向

+0

感谢CQM。我会尝试一下,然后来找你。 – Allen 2012-08-08 20:31:55

+0

@Allen也许你会发现这个链接有用http://www.vogella.com/articles/AndroidListView/article.html – luanjot 2012-08-08 20:44:59

0

为什么你计划使用ListView? A TableLayout加上ScrollView对您来说可能更容易。

0

它好工作对我来说,做出自己的ArrayAdapter。 那里你得到的方法

getView() 你可能会覆盖。

你在那里使用 LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 来夸大你自己的视图,你在布局中定义为XML。 你可以在这个方法中直接填充视图元素。

在我的代码我做了一个图标,两个文本字段和一个进度条的布局。这样看:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
... > 

    <ImageView 
     android:id="@+id/inventory_item_icon" 
     .../> 

    <TextView 
     android:id="@+id/inventory_item_titel" 
     .../> 


    <TextView 
     android:id="@+id/inventory_status_textfield" 
     ... /> 

    <TextView 
     android:id="@+id/inventory_item_info" 
     ... /> 

    <ProgressBar 
     android:id="@+id/inventory_item_status" 
     ... /> 

</RelativeLayout> 

它很简单,直接在getView填补他们()后 我的代码如下所示:

public View getView(int position, View convertView, ViewGroup parent) { 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View rowView = inflater.inflate(R.layout.inventory_icon_text_text, parent, false); 
TextView textViewTitel = (TextView) rowView.findViewById(R.id.inventory_item_titel); 
TextView textViewInfo = (TextView) rowView.findViewById(R.id.inventory_item_info); 
ImageView imageView = (ImageView) rowView.findViewById(R.id.inventory_item_icon); 

textViewTitel.setText(values[position]); 
    String s = values[position]; 
    if (s.equals("A")) { 
     imageView.setImageResource(R.drawable.a); 
     textViewInfo.setText("This is case A!"); 
    } else if (s.equals("B")) { 
    return rowView; 
} 

这样你就可以直接解决各个视图元素。 如果你想要有不同的视图布局,你可以尝试在膨胀布局之前检查一下。谢谢你可以选择你想使用哪一个。

希望这可以帮助你! 托比亚斯