問題描述
給ListView的item指定不同的布局,Adapter繼承BaseAdapter并復寫以下兩個方法:
/**
* 返回 有幾種item布局
* @return
*/
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
@Override
public int getItemViewType(int position) {
if (condition) {
return TYPE_1;
}
return TYPE_2;
}
我的TYPE_1和TYPE_2是這樣定義的:
private final int TYPE_COUNT = 2;
private final int TYPE_1 = 1;
private final int TYPE_2 = 2;
然后在getView中通過getItemViewType(position)
返回值來判斷加載哪種布局:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 viewHolder1 = null;
ViewHolder2 viewHolder2 = null;
int currentType = getItemViewType(position);
if (convertView == null) {
switch (currentType) {
case TYPE_1:
convertView = mLayoutInflater.inflate(R.layout.simple_item_1, null);
viewHolder1 = new ViewHolder1();
viewHolder1.imageView = (ImageView) convertView.findViewById(R.id.imgview);
convertView.setTag(viewHolder1);
break;
case TYPE_2:
convertView = mLayoutInflater.inflate(R.layout.simple_item_2, null);
viewHolder2 = new ViewHolder2();
viewHolder2.textView = (TextView) convertView.findViewById(R.id.textview);
convertView.setTag(viewHolder2);
break;
}
} else {
switch (currentType) {
case TYPE_1:
viewHolder1 = (ViewHolder1) convertView.getTag();
break;
case TYPE_2:
viewHolder2 = (ViewHolder2) convertView.getTag();
break;
}
}
...
return convertView;
}
這樣看起來好像是沒什么問題,但運行起來就會崩潰:
FATAL EXCEPTION: main
Process: com.lc.learndemo, PID: 2374
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6643)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:446)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
問題分析
報錯原因是數組越界,但我只傳入了一個簡單的List,不會有問題,其他地方也沒用到數組啊。
其實仔細分析報錯信息,可以知道ListView item在布局過程中運行報錯,這就懷疑ItemType是否有問題了。最后鎖定到 TYPE_1和TYPE_2,當時以為這只是不同類型布局的一個標識,隨便取一個值就可以了,誰知問題恰巧就出在這里。
問題定位
跟蹤getItemViewType源碼可以看到:
/**
* Get the type of View that will be created by {@link #getView} for the specified item.
*
* @param position The position of the item within the adapter's data set whose view type we
* want.
* @return An integer representing the type of View. Two views should share the same type if one
* can be converted to the other in {@link #getView}. Note: Integers must be in the
* range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can
* also be returned.
* @see #IGNORE_ITEM_VIEW_TYPE
*/
int getItemViewType(int position);
其中有這樣一句:
Note: Integers must be in the range 0 to {@link #getViewTypeCount} - 1.
返回值只能在 0 ~ (getViewTypeCount - 1) 之間
而我的Demo中Item TYPE卻是從1開始的,這樣,問題就定位到了,接下來就是解決了。
問題解決
如下,Item type 從 0 開始即可:
private final int TYPE_COUNT = 2;
private final int TYPE_1 = 0;
private final int TYPE_2 = 1;
總結
源碼是最好的學習資源,遇到類似無厘頭Bug,從源碼中總能找到答案。