我正在尝试实现一个自定义 ListView ,其中包含分隔符。这是代码:
单标题:
public class SingleTitle {
String mTitle;
int[] mInterval;
public SingleTitle(String title, int[] interval) {
mTitle = title;
mInterval = interval;
}
public String getTitle() {
return mTitle;
}
public int[] getInterval() {
return mInterval;
}
}
标题类别:
public class TitleCategory {
String mTitle;
public TitleCategory(String title) {
mTitle = title;
}
public String getTitle() {
return mTitle;
}
}
list_category_divider.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/list_item_section_text"
layout="@android:layout/preference_category" />
</ LinearLayout >
返回列表的方法:
public static List<Object> getStructuredTitles(Context context) {
List<Object> list = new ArrayList<Object>();
String[] mTitles = getCategoriesTitles(context);
String[] mParts = getPartsTitles(context);
list.add(new TitleCategory(mParts[0]));
list.add(new SingleTitle(mTitles[0], CATEGORIES_INTERVALS[0]));
list.add(new TitleCategory(mParts[1]));
list.add(new SingleTitle(mTitles[1], CATEGORIES_INTERVALS[1]));
list.add(new SingleTitle(mTitles[2], CATEGORIES_INTERVALS[2]));
list.add(new SingleTitle(mTitles[3], CATEGORIES_INTERVALS[3]));
list.add(new SingleTitle(mTitles[4], CATEGORIES_INTERVALS[4]));
list.add(new TitleCategory(mParts[2]));
list.add(new SingleTitle(mTitles[5], CATEGORIES_INTERVALS[5]));
list.add(new SingleTitle(mTitles[6], CATEGORIES_INTERVALS[6]));
list.add(new SingleTitle(mTitles[7], CATEGORIES_INTERVALS[7]));
list.add(new SingleTitle(mTitles[8], CATEGORIES_INTERVALS[8]));
list.add(new SingleTitle(mTitles[9], CATEGORIES_INTERVALS[9]));
list.add(new SingleTitle(mTitles[10], CATEGORIES_INTERVALS[10]));
list.add(new TitleCategory(mParts[3]));
list.add(new SingleTitle(mTitles[11], CATEGORIES_INTERVALS[11]));
return list;
}
我的自定义适配器:
public class TitlesAdapter extends BaseAdapter {
private Context context;
private List<Object> entries;
public TitlesAdapter(Context context, List<Object> entries) {
this.context = context;
this.entries = entries;
}
@Override
public int getCount() {
return entries.size();
}
@Override
public Object getItem(int position) {
return entries.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Object entry = entries.get(position);
boolean isCategory = (entry instanceof TitleCategory);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(isCategory ? R.layout.list_category_divider : R.layout.list_items_simple, null);
}
if (isCategory) {
TitleCategory tc = (TitleCategory)entry;
convertView.setClickable(false);
Log.d("DBG", "Entry: " + position + " | " + tc.getTitle());
TextView tv = (TextView) convertView.findViewById(android.R.id.simple_list_item_2);
if (tv != null) tv.setText(tc.getTitle());
} else {
SingleTitle tc = (SingleTitle)entry;
Log.d("DBG", "Entry: " + position + " | " + tc.getTitle());
TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
if (tv != null)
tv.setText(tc.getTitle());
}
return convertView;
}
public List<Object> getList() {
return entries;
}
public void setList(List<Object> newentries) {
entries = newentries;
notifyDataSetChanged();
}
}
我的问题: 该应用程序最初可以很好地绘制列表,但是当我上下滚动列表时,它会弄乱条目。但是在 logcat 中生成的输出是正确的,但是 Android 把列表弄乱了。任何的想法?
更新: