我想實現一個自定義的ListView,其中有分隔符。這是代碼:帶分隔符的自定義ListView變得瘋狂
SINGLETITLE:
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;
}
}
TitleCategory:
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會弄亂列表。任何想法?
UPDATE:
你能發表截圖嗎? – 2012-04-15 18:56:13
完成;)10chars – Gnufabio 2012-04-15 19:02:31