我有一个listview并且排列得当。我需要添加列标题。下面我发布了处理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.xml
<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>
ShowData.java
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
/**
* Created by Akash on 3/13/2016.
*/
public class ShowData extends ListActivity {
private VivzHelper helper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VivzHelper empClick = new VivzHelper(getApplicationContext());
Cursor cursor = empClick.getDetails();
if (cursor != null)
getListView().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));
}
}
答案 0 :(得分:2)
如果您想在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
答案 1 :(得分:0)
您可以使用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;
}
答案 2 :(得分:0)
将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>