我创建了一个自定义适配器来制作分段的ListView,但是Item似乎是自我复制的。
它将显示如下:
Title 1
1 Item 1
2 Item 2
3 Item 3
4 Item 4
5 Item 5
Title 2
6 Item 6
7 Item 7
8 Item 8
9 Item 9
10 Item 10
Title 3
11 Item 11
12 Item 12
13 Item 13
1 Item 1 //Should be 14 Item 14
15 Item 15
如何解决这个问题?
在MainActivity中使用适配器
adapter.addSection("Title 1");
adapter.addItem("1", "Item 1");
adapter.addItem("2", "Item 2");
adapter.addItem("3", "Item 3");
adapter.addItem("4", "Item 4");
adapter.addItem("5", "Item 5");
adapter.addSection("Title 2");
adapter.addItem("6", "Item 6");
adapter.addItem("7", "Item 7");
adapter.addItem("8", "Item 8");
adapter.addItem("9", "Item 9");
adapter.addItem("10", "Item 10");
adapter.addSection("Title 3");
adapter.addItem("11", "Item 11");
adapter.addItem("12", "Item 12");
adapter.addItem("13", "Item 13");
adapter.addItem("14", "Item 14");
adapter.addItem("15", "Item 15");
item_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:orientation="horizontal" >
<TextView
android:id="@+id/textNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:gravity="center"
android:layout_weight="10"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Text"
android:layout_weight="90"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
section_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textSection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Section Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends BaseAdapter{
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private LayoutInflater inflater;
private List<ItemRow> itemRow = new ArrayList<ItemRow>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private ItemRow ir;
public CustomAdapter(Context context){
inflater = LayoutInflater.from(context);
}
public void addItem(String no, String s){
ir = new ItemRow();
ir.setNumbering(no);
ir.setDetail(s);
itemRow.add(ir);
notifyDataSetChanged();
}
public void addSection(String title){
ir = new ItemRow();
ir.setTitle(title);
sectionHeader.add(itemRow.size());
itemRow.add(ir);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return itemRow.size();
}
@Override
public Object getItem(int position) {
return itemRow.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
ViewHolder holder;
int rowType = getItemViewType(position);
ItemRow row = itemRow.get(position);
if(convertView == null){
switch(rowType){
case TYPE_ITEM:
view = inflater.inflate(R.layout.item_text, parent, false);
holder = new ViewHolder();
holder.number = (TextView) view.findViewById(R.id.textNumber);
holder.detail = (TextView) view.findViewById(R.id.textItem);
holder.number.setText(row.getNumbering());
holder.detail.setText(row.getDetail());
view.setTag(holder);
break;
case TYPE_SEPARATOR:
view = inflater.inflate(R.layout.section_text, parent, false);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.textSection);
holder.title.setText(row.getTitle());
view.setTag(holder);
break;
}
} else{
view = convertView;
holder = (ViewHolder) view.getTag();
}
return view;
}
private static class ViewHolder{
public TextView title, number, detail;
}
}
ANSWER
所以我终于找到了解决方案。 setText应该在getView
中的if else之后@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;// = new ViewHolder();;
int rowType = getItemViewType(position);
ItemRow row = itemRow.get(position);
if(convertView == null){
switch(rowType){
case TYPE_ITEM:
convertView = inflater.inflate(R.layout.item_text, parent, false);
holder = new ViewHolder();
holder.number = (TextView) convertView.findViewById(R.id.textNumber);
holder.detail = (TextView) convertView.findViewById(R.id.textItem);
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
convertView = inflater.inflate(R.layout.section_text, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.textSection);
convertView.setTag(holder);
break;
}
} else{
holder = (ViewHolder) convertView.getTag();
}
if(rowType == TYPE_ITEM){
holder.number.setText(row.getNumbering());
holder.detail.setText(row.getDetail());
}else
holder.title.setText(row.getTitle());
return convertView;
}