Android:包含不同项目的列表视图的xml布局

use*_*621 4 layout android listview

我正在阅读本教程http://android.amberfog.com/?p=296.我想创建一个不同类型的行的Listview.我理解如何创建适配器,但xml布局呢?所以我定义了像这样的xml布局:

<ListView/>

<TextView android:id="@+id/id1" />

<TextView android:id="@+id/id2" />

<ImageView android:id="@+id/id3" />

<TextView android:id="@+id/id4" />
Run Code Online (Sandbox Code Playgroud)

如果一行只使用布局的某些元素(仅一些文本视图)而另一行可能使用其他元素,那么它是否会成为一个问题(性能)?我不明白我的是否是定义xml的正确方法,或者我是否必须为每种类型的行创建不同的布局.

先感谢您

编辑:现在我有一个零点异常.

来自适配器的java代码:

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
int type = getItemViewType(position);

if (convertView == null) {
    holder = new ViewHolder();

    convertView = mInflater.inflate(R.layout.listview_main, null);
    holder.textView_title = (TextView)convertView.findViewById(R.id.listview1);

    convertView.setTag(holder);
} else {
    holder = (ViewHolder)convertView.getTag();
}

**holder.textView_title.setText("aaaa");** //NULL POINT EXCEPTION HERE

return convertView;
Run Code Online (Sandbox Code Playgroud)

}

class ViewHolder {
    public TextView textView_title;
}
Run Code Online (Sandbox Code Playgroud)

xml 1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/main_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="left"
              android:layout_margin="0dp">

    <!-- android:background="#0094ff" -->

    <ListView
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fastScrollEnabled="true"
            android:scrollbarStyle="insideInset"
            android:textFilterEnabled="false"
            android:divider="@null"
            android:layout_margin="0dp"
            android:paddingTop="0dp"
            android:paddingBottom="0dp"
            android:paddingLeft="15dp"
            android:paddingRight="22dp"/>



</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

XML2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="left"
    android:layout_margin="0dp">

    <TextView
        android:id="@+id/listview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="7dp"
        android:paddingBottom="0dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:textSize="18sp"
        android:textColor="#000000"
        android:lines="1">
    </TextView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Rag*_*dan 13

你需要覆盖getViewItemTypegetViewTypeCount.您还需要具有自定义布局.

getItemViewType(int position) - 根据位置返回您应使用的布局类型的信息.

您应该查看链接中的视频.

http://www.youtube.com/watch?v=wDBM6wVEO70

private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2; 
Run Code Online (Sandbox Code Playgroud)

然后

int type;
@Override
public int getItemViewType(int position) {

    if (position== 0){
        type = TYPE_ITEM1;
    } else if  (position == 1){
        type = TYPE_ITEM2;
    }
    else
    {
         type= TYPE_ITEM3 ;
    }
    return type;
}

 @Override
 public int getViewTypeCount() {
        return 3; 
 }
@Override  
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
  // instead of if else you can use a case
   if (row  == null) {
    if (type == FIRST_TYPE) {
            //infalte layout of type1
      }
    if (type == SECOND_TYPE) {
            //infalte layout of type2
    }  else {
            //infalte layout of normaltype
 }
} 
Run Code Online (Sandbox Code Playgroud)