问题描述
给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,从源码中总能找到答案。
网友评论