Android ListView標頭我有ListView,它有一些事件。事件按日排序,我希望每天都有標題日期,然后在下面收聽事件。以下是我填充該列表的方式:ArrayList<TwoText> crs = new ArrayList<TwoText>();crs.add(new TwoText("This will be header", event.getDate()));for (Event event : events) {
crs.add(new TwoText(event.getStartString() + "-" + event.getEndString(), event.getSubject()));}arrayAdapter = new TwoTextArrayAdapter(this, R.layout.my_list_item, crs);lv1.setAdapter(arrayAdapter);這就是我的類TwoText看起來的樣子:public class TwoText {
public String classID;
public String state;
public TwoText(String classID, String state) {
this.classID = classID;
this.state = state;
}}這就是我的TwoTextArrayAdapter類的外觀:import java.util.ArrayList;import android.app.Activity;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;public class TwoTextArrayAdapter extends ArrayAdapter<TwoText> {
private ArrayList<TwoText> classes;
private Activity con;
TextView seperator;
public TwoTextArrayAdapter(Activity context, int textViewResourceId, ArrayList<TwoText> classes) {
super(context, textViewResourceId, classes);
this.con = context;
this.classes = classes;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.my_list_item, null);
}我現在所做的是我正在添加標題,就像常規列表對象一樣,但我喜歡它作為標題,在我的情況下有一個日期。我嘗試在必要時隱藏它并在必要時顯示它但我只是弄亂了我的其余代碼。我嘗試了更多的教程,但他們也有同樣的效果。任何人都可以指導我如何做到這一點嗎?
3 回答

慕后森
TA貢獻1802條經驗 獲得超5個贊
作為替代方案,有一個很好的第三方庫專為此用例而設計。因此,您需要根據存儲在適配器中的數據生成標頭。它們被稱為Rolodex適配器并與之一起使用ExpandableListViews
。它們可以很容易地進行自定義,就像帶有標題的普通列表一樣。
使用OP的Event
對象并知道標題是基于Date
與之關聯的...代碼看起來像這樣:
活動
//There's no need to pre-compute what the headers are. Just pass in your List of objects. EventDateAdapter adapter = new EventDateAdapter(this, mEvents); mExpandableListView.setAdapter(adapter);
適配器
private class EventDateAdapter extends NFRolodexArrayAdapter<Date, Event> { public EventDateAdapter(Context activity, Collection<Event> items) { super(activity, items); } @Override public Date createGroupFor(Event childItem) { //This is how the adapter determines what the headers are and what child items belong to it return (Date) childItem.getDate().clone(); } @Override public View getChildView(LayoutInflater inflater, int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { //Inflate your view //Gets the Event data for this view Event event = getChild(groupPosition, childPosition); //Fill view with event data } @Override public View getGroupView(LayoutInflater inflater, int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { //Inflate your header view //Gets the Date for this view Date date = getGroup(groupPosition); //Fill view with date data } @Override public boolean hasAutoExpandingGroups() { //This forces our group views (headers) to always render expanded. //Even attempting to programmatically collapse a group will not work. return true; } @Override public boolean isGroupSelectable(int groupPosition) { //This prevents a user from seeing any touch feedback when a group (header) is clicked. return false; }}
添加回答
舉報
0/150
提交
取消