У меня есть вид списка, и я пытаюсь установить для него заголовок, чтобы он выглядел как заголовок таблицы. Вот XML-файл из моего списка просмотра строки:Android Добавление динамического заголовка ListView
PS: Я удалил некоторые коды, чтобы сократить Question
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
, и мой XML-файл из моего списка заголовков вид:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date" />
<TextView
android:id="@+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Category" />
А класс, где я заселить вид списка:
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.trans_header_layout, null);
listview.addHeaderView(listHeaderView);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.trans_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
Однако при использовании этих кодов, вот вывод, который я получил:
Заголовок не совпадает со столбцом, расположенным ниже. Интересно, есть ли альтернативный способ добавления заголовка в список, чтобы он выглядел как заголовок таблицы.
Заранее спасибо.
EDIT
Мой transaction_rec.XML где расположен ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date" />
<TextView
android:id="@+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category" />
<TextView
android:id="@+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
И мой transactionRec.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction_rec);
context = this;
listview = (ListView) findViewById(R.id.listview);
//Code to get data from database
}
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.transaction_rec,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
}
И выход я получил в настоящее время:
Имхо вы можете использовать ширину заработной платы. Вот, вероятно, решение http://stackoverflow.com/a/4517358/619673 – deadfish
Но есть ли способ установить ширину в соответствии с столбцом строк ниже? –
Итак, вы должны verride onMeasure для компоновки заголовков и использовать те же параметры для макета строки. Однако заработная плата проще (она собирается установить ширину оплаты для элементов в заголовке и строке позиций. Скажем: заработная плата равна 1 для заголовков (у нас есть 5) .20, .20, .20, .20, .20 и для макета строки тоже те же значения.) – deadfish