Android:如何在自定义listview中添加列标题

Android:如何在自定义listview中添加列标题,android,Android,我有一个列表视图,它是正确安排的。我需要添加列标题。下面我发布了处理listview排列的display.xml文件。但是没有标题 Display.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

我有一个列表视图,它是正确安排的。我需要添加列标题。下面我发布了处理listview排列的
display.xml
文件。但是没有标题

Display.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F3CAE5"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="8dp" >

    <TextView

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000"
        android:text="Ser No."/>
    <TextView
        android:id="@+id/id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#000" />

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:textColor="#000" />

    <TextView
        android:id="@+id/add"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:textColor="#000" />

</LinearLayout>

如果要在listView顶部显示标题,可以使用标题。删除它,而不是在listView上方包含标题布局。将其保留在另一个xml资源(
header.xml
)中

然后,在设置适配器时,还可以像下面这样将标题添加到列表中

ListView listView = getListView();
View header = getLayoutInflater().inflate(R.layout.header, null);
listView.addHeaderView(header);
listView.setAdapter(new android.support.v4.widget.SimpleCursorAdapter(this, R.layout.display, cursor, new String[]{
                        "_id", "Name", "Address"
                }, new int[]{R.id.id, R.id.name, R.id.add}, 0));
(我假设在方法getListView()中,您正在执行以下操作

private ListView getListView(){
return ListView listView = (ListView) findViewById(R.id.list);
}
这应该是您的
list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </ListView>

</LinearLayout>

而header.xml将是另一个布局,您可以在其中定义要显示在列表顶部的任何内容

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="Street" 
    android:layout_gravity="left"
    android:background="#88FF0000"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="number"
    android:layout_gravity="right" 
    android:background="#8800FF00"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="name"
    android:layout_gravity="right" 
    android:background="#8800FF00"/>

</LinearLayout


您可以使用RecyclerView.Adapter来实现这一点。
RecyclerView.Adapter具有getItemViewType()函数。

示例

@Override
public int getItemViewType(int position)
{
    if(position == 0)
        return LIST_HEADER;
    else if(position > 0)
        return LIST_ITEM;
    return -1;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    if (viewType == LIST_HEADER)
    {
        View view = LayoutInflater.from(mContext).inflate(R.layout.your_header_layout, parent, false);
        return view;
    }
    else if (viewType == LIST_ITEM)
    {
        View view = LayoutInflater.from(mContext).inflate(R.layout.your_item_layout, parent, false);
        return view;
    }
    return null;
}

将Display.xml名称更改为header.xml

然后使用include.添加布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/header"/>
    <ListView
        android:id="@+id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </ListView>

</LinearLayout>


尝试创建listheader.xml并使用与display.xml相同的权重,然后在代码中设置listview.addHeaderView。为什么不将tableLayout与ScrollView一起使用而不是使用list view?header.xml的内容应该是什么?无论您希望在列表顶部显示什么。如果我没有,我认为应该是您之前包含的布局误解,我需要有三列标题,序列号、名称和位置,所以告诉我如何形成xml文件我已经更新了代码,将标题包含在三列中
@Override
public int getItemViewType(int position)
{
    if(position == 0)
        return LIST_HEADER;
    else if(position > 0)
        return LIST_ITEM;
    return -1;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    if (viewType == LIST_HEADER)
    {
        View view = LayoutInflater.from(mContext).inflate(R.layout.your_header_layout, parent, false);
        return view;
    }
    else if (viewType == LIST_ITEM)
    {
        View view = LayoutInflater.from(mContext).inflate(R.layout.your_item_layout, parent, false);
        return view;
    }
    return null;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/header"/>
    <ListView
        android:id="@+id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </ListView>

</LinearLayout>