当前位置: 代码迷 >> Android >> 一、机制
  详细解决方案

一、机制

热度:211   发布时间:2016-04-28 02:05:56.0
Android ListView 常见问题与使用总结

一、机制

ListView机制 - 先执行getCount,然后执行getView。如果getCount返回0,不执行getView
Gallery convertView不会复用,每次都返回NULL

二、问题整理

ListView最下面一个item没有横线(Divider)?
listview的布局高使用wrap_content,应该使用fill_parent

去除行与行之间的黑线:
List.setDivider(null); 

切记ListView高度不能设置成wrap_content
会导致多次整个页面刷新


动态修改Item高度 - ItemRootView.getLayoutParams().height = heightValue;
     尝试使用margin、Child height都无效

Item Root设置高度失效,可以通过设置子视图高度解决。(ListView ItemView 子视图设置margin无效)
两层,内层使用margin,外层使用FrameLayout失效,改成Linearlayout有效。例如Htc G12 就会失效。


三、常用属性
ListView Item selector
item.xml 外层LinearLayout设置selector.xml后,内层再设置bakcground没有选中效果。
     因为点击后下层其实改变了,但是被上层给盖住了,这是常识。。。。。
点击父视图,子视图是否被选中,例如button

去除选中时的黄色底色
android:listSelector="#00000000" 
android:listSelector="@null"   // 这样设置无效



1. 是否显示ScrollBar

2. 关闭OverScroll , 有些手机上OverScroll有类似FadingEdge效果
listView.setOverScrollMode(View.OVER_SCROLL_NEVER);

3. 去除拖动时默认的黑色背景:
android:cacheColorHint="#00000000"


5. ListView刷新后自动滚到最底部:
List.setSelection(msgList.getAdapter().getCount()-1); 

6. 去除ListView滑到顶部和底部时边缘的黑色阴影:
android:fadingEdge="none"  





** 异常
get item type 从0开始,出现以下异常说明,getItemViewType 返回类型个数超过getViewTypeCount。
当前异常是getItemViewType 返回3种类型的id,而getViewTypeCount = 2


java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
     at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:7188)
     at android.widget.ListView.measureHeightOfChildren(ListView.java:1338)
     at android.widget.ListView.onMeasure(ListView.java:1238)
     at android.view.View.measure(View.java:15729)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4854)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
     at android.view.View.measure(View.java:15729)
     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
     at android.view.View.measure(View.java:15729)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4854)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
     at android.view.View.measure(View.java:15729)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4854)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
     at android.view.View.measure(View.java:15729)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4854)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2344)
     at android.view.View.measure(View.java:15729)
     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1948)
     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1300)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4498)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
     at android.view.Choreographer.doFrame(Choreographer.java:532)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
     at android.os.Handler.handleCallback(Handler.java:725)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:5105)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:511)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
     at dalvik.system.NativeStart.main(Native Method)



onScroll 滚动状态判断
onScrollListener
SCROLL_STATE_IDLE 滑动后静止
SCROLL_STATE_FLING 手指离开屏幕后,惯性滑动
SCROLL_STATE_TOUCH_SCROLL 手指在屏幕上滑动

测试相关
ActivityThread中代码,此代码调用onSaveInstanceState。mInstrumentation负责调用on***方法
mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);



GridVie 不显示滚动条

android:scrollbars="none"与

setVerticalScrollBarEnabled(true);