ViewPager:java.lang.IllegalArgumentException:如果pointerIndex超出范围 [英] ViewPager: java.lang.IllegalArgumentException: pointerIndex out of range
问题描述
我在写一个code基于什么建议来处理触摸事件<一href="http://neevek.net/posts/2013/10/13/implementing-onInterceptTouchEvent-and-onTouchEvent-for-ViewGroup.html"相对=nofollow>此处。我使用ViewPager作为一个ViewGroup和ListView(我知道 #thisbad )为片段的子视图。
I am writing an code to handle touch event based on what suggested here. I am using ViewPager as an ViewGroup and ListView(I know #thisbad) as child view of Fragment.
这就是我想实现:
- 在检测的多点触控事件的子视图
- 然后通过触摸控制于母公司
不过,虽然通过监听事件从子视图到父视图它给以下错误:
but While passing listener event from child View to Parent view it giving following error:
E/AndroidRuntime(11414): java.lang.IllegalArgumentException:pointerIndex out of range
E/AndroidRuntime(11414): at android.view.MotionEvent.nativeGetAxisValue(Native Method)
E/AndroidRuntime(11414): at android.view.MotionEvent.getX(MotionEvent.java:1979)
E/AndroidRuntime(11414): at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
E/AndroidRuntime(11414): at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:91)
E/AndroidRuntime(11414): at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:219)
E/AndroidRuntime(11414): at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1971) ..................
E/AndroidRuntime(11414): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5588)
E/AndroidRuntime(11414): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5634)
E/AndroidRuntime(11414): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
E/AndroidRuntime(11414): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
E/AndroidRuntime(11414): at android.view.Choreographer.doFrame(Choreographer.java:542)
我问过其他一些职位有像和的here 但他们所使用的指针索引做服用点但对我来说,我只是将控制传递给父(ViewPager)的看法,现在。
I checked with some other post have same issue like and here but all they are using Pointer indexing to do somehting but in my case I am just passing control to parent(ViewPager) view now.
分析:在这里,我检查用的FrameLayout而不是列表视图中的儿童片段。和它的工作没有任何提到issue..but不与ListView控件。
Analysis: Here I checked to use FrameLayout instead to Listview in Child Fragment. and it's working without any mentioned issue..but not with ListView.
设备信息: OS V4.4.4 S5
Device Info: OS V4.4.4 S5.
任何建议!
推荐答案
我不知道为什么MotionEventCompat呼吁MotionEventCompatEclair,就像我在code看见,有MotionEventCompatHoneycomb过载,但我有同样的问题与摩托罗拉XT1040在安卓4.4.4。
I don't know why MotionEventCompat is calling MotionEventCompatEclair, as I saw in the code, there are a MotionEventCompatHoneycomb overload, but I had the same problem with Moto XT1040 in android 4.4.4.
我的解决方案是创建我的版本ViewPager的(我只是复制从Android源整个类),并在MotionEvent.ACTION_MOVE的情况下,方法onInterceptTouchEvent(MotionEvent EV)从更改初始行:
The solution for me was create my version of ViewPager(I just copied the entire class from android source) and in the method onInterceptTouchEvent(MotionEvent ev) in the case of MotionEvent.ACTION_MOVE change the initial lines from:
final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER) {
// If we don't have a valid id, the touch down wasn't on content.
break;
}
final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float yDiff = Math.abs(y - mInitialMotionY);
到
final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER) {
// If we don't have a valid id, the touch down wasn't on content.
break;
}
final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
final float x = ev.getX(pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = ev.getY(pointerIndex);
final float yDiff = Math.abs(y - mInitialMotionY);
唯一的变化是在x和y的变量,我改为直接调用MotionEvent的getX和方法的getY,忽略MotionEventCompat。
The only change was in the x and y variables, I changed to call the MotionEvent getX and getY method directly, ignoring the MotionEventCompat.
我的应用程序是最小的API 14,如果你是从14配套的东西previous,我建议你打电话从您的版本最接近的MotionEventCompat。你只需要prevent从调用MotivoEventCompatEclair版本。
My app was minimum api 14, if you are supporting something previous from 14, I suggest to you call the closest MotionEventCompat from your version. You just need to prevent from calling the MotivoEventCompatEclair version.
这篇关于ViewPager:java.lang.IllegalArgumentException:如果pointerIndex超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!