セパレーターを持つカスタム 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 はリストを台無しにします。何か案が?
アップデート: