8月 252016
 

要实现可拖动的九宫格,网上搜了下,搜到比较靠谱的是这个版本,原理、实现都写的很清楚,但有个问题,用到GridView时,写adapter时一般都会使用ViewHolder的方式,即adapter使用BaseAdapter,adapter中保存数据,而调用getView方法时,若是第一次调用则将可复用的数据保存到一个ViewHolder类中,后续就不用再重新创建View;或者,adapter中每次根据数据创建view。
由于公司所用的客户端框架限制,我在实现可拖动的九宫格时,在getView方法中,拿到的数据就是一个个已经创建好的View对象,但按照上面提到的xiaanming的拖动实现方式,我将这些已有的View对象在getView中返回、加入到GridView后,发现显示没问题,但是拖动之后,View的显示顺序会乱掉,而且会出现第一次点击某个View时对应回调方法没有响应,但第二次点击时则会响应第一次点击、第二次点击两个回调方法。
为解决这问题,在github上发现如下可拖动九宫格的另一种实现,尝试重写adapter类后解决了该问题,发现这个跟stableid有关。我一开始写的adapter大概样子如下; Continue reading »

8月 242016
 

app上线前遇到一个很诡异的问题,同一款型号的手机,一台手机没问题,另一台手机则在显示首页后假死、然后直接闪退。经过排查,确定是自定义GridView惹的祸,异常信息如下:

 java.lang.IllegalArgumentException: parameter must be a descendant of this view
 at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:5353)
 at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:5282)
 at android.view.ViewGroup$ViewLocationHolder.init(ViewGroup.java:7755)
 at android.view.ViewGroup$ViewLocationHolder.obtain(ViewGroup.java:7689)
 at android.view.ViewGroup$ChildListForAccessibility.init(ViewGroup.java:7624)
 at android.view.ViewGroup$ChildListForAccessibility.obtain(ViewGroup.java:7592)
 at android.view.ViewGroup.addChildrenForAccessibility(ViewGroup.java:1927)
 at android.view.ViewGroup.onInitializeAccessibilityNodeInfoInternal(ViewGroup.java:2982)
 at android.widget.AdapterView.onInitializeAccessibilityNodeInfoInternal(AdapterView.java:983)
 at android.widget.AbsListView.onInitializeAccessibilityNodeInfoInternal(AbsListView.java:1509)
 at android.widget.GridView.onInitializeAccessibilityNodeInfoInternal(GridView.java:2361)
 at android.view.View.onInitializeAccessibilityNodeInfo(View.java:6151)
 at android.view.View.createAccessibilityNodeInfoInternal(View.java:6110)
 at android.view.View.createAccessibilityNodeInfo(View.java:6095)
 at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:145)
 at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:119)
 at android.view.View.onInitializeAccessibilityEventInternal(View.java:6047)
 at android.widget.AdapterView.onInitializeAccessibilityEventInternal(AdapterView.java:994)
 at android.view.View.onInitializeAccessibilityEvent(View.java:6035)
 at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:5896)
 at android.view.View.sendAccessibilityEventUnchecked(View.java:5881)
 at android.view.View$SendViewStateChangedAccessibilityEvent.run(View.java:22468)
 at android.os.Handler.handleCallback(Handler.java:743)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:150)
 at android.app.ActivityThread.main(ActivityThread.java:5546)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)

Continue reading »