我对Android和Java比较陌生,所以如果这是一个愚蠢的问题请原谅我。我正在使用Jeff Sharkeys SeperatedListAdapter,我想创建一个如下所示的列表,其中 HEAD item_complex.xml,list_item.xml,item_complex.xml另一个 HEADER ...等等,但我无法理解为实现这一目标需要做些什么。很可能是由于我缺乏android和java知识。有人可以帮帮我吗?
如果它有帮助......代码......
package com.biosonik.iceland.news;
import java.util.LinkedHashMap;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import com.biosonik.iceland_planner.R;
public class SeparatedListAdapter extends BaseAdapter {
public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;
public SeparatedListAdapter(Context context) {
headers = new ArrayAdapter<String>(context, R.layout.list_header);
}
public void addSection(String section, Adapter adapter) {
this.headers.add(section);
this.sections.put(section, adapter);
}
public Object getItem(int position) {
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return section;
if(position < size) return adapter.getItem(position - 1);
// otherwise jump into next section
position -= size;
}
return null;
}
public int getCount() {
// total together all sections, plus one for each section header
int total = 0;
for(Adapter adapter : this.sections.values())
total += adapter.getCount() + 1;
return total;
}
@Override
public int getViewTypeCount() {
// assume that headers count as one, then total all sections
int total = 1;
for(Adapter adapter : this.sections.values())
total += adapter.getViewTypeCount();
return total;
}
@Override
public int getItemViewType(int position) {
int type = 1;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return TYPE_SECTION_HEADER;
if(position < size) return type + adapter.getItemViewType(position - 1);
// otherwise jump into next section
position -= size;
type += adapter.getViewTypeCount();
}
return -1;
}
public boolean areAllItemsSelectable() {
return false;
}
@Override
public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}
public View getView(int position, View convertView, ViewGroup parent) {
int sectionnum = 0;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return headers.getView(sectionnum, convertView, parent);
if(position < size) return adapter.getView(position - 1, convertView, parent);
// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}
public long getItemId(int position) {
return position;
}
}
...活性
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.planner_list);
List<Map<String,?>> security = new LinkedList<Map<String,?>>();
week34.add(createItem("62","Monday 19th Novermber","C1P1 Change-Over Day 1", "text text text text",String.valueOf(R.drawable.lsnow),"9C"));
week35.add(createItem("52","Monday 19th Novermber", "C1P1 Change-Over Day 2", "text text text text",String.valueOf(R.drawable.hsnow),"12C"));
// create our list and custom adapter
SeparatedListAdapter adapter = new SeparatedListAdapter(this);
adapter.addSection("Week 34",
new SimpleAdapter(DisplayNewsActivity.this, week34, R.layout.list_complex,
new String[] {ITEM_UID, ITEM_DATE, ITEM_HEADLINE, ITEM_GRAB_LINE, ITEM_WEATHER_TEMP, ITEM_WEATHER_ICON }, new int[] {R.id.uid, R.id.date, R.id.headline, R.id.grab_line,R.id.weather_icon,R.id.weather_temp }));
adapter.addSection("UNWANTED HEADER", new ArrayAdapter<String>(this,
R.layout.list_item, new String[] { "SIMPLE LIST ENTRY" }));
adapter.addSection("Week 35",
new SimpleAdapter(DisplayNewsActivity.this, week35, R.layout.list_complex,
new String[] {ITEM_UID, ITEM_DATE, ITEM_HEADLINE, ITEM_GRAB_LINE, ITEM_WEATHER_TEMP, ITEM_WEATHER_ICON }, new int[] {R.id.uid, R.id.date, R.id.headline, R.id.grab_line,R.id.weather_icon,R.id.weather_temp }));
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
答案 0 :(得分:0)
在getViewTypeCount()
中,为什么假设标头计为一个?在我看到这种方法的方式中,每个部分都会有两种不同的视图,所以例如,对于2个部分,你将有4种类型的视图而不是3种,因为它是由这段代码(为我的英语道歉)