在触摸时更改gridview中项目的背景颜色

时间:2012-12-10 20:51:16

标签: android gridview ontouchlistener

我有一个gridView布局,其中填充了textView元素。我有一个gridView的onTouch()实现,其中被触摸的单元格需要更改背景颜色。下面是我的代码,但它不起作用,当我触摸时VM关闭。

gridView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {

            int action = me.getActionMasked();
            float currentXPosition = me.getX();
            float currentYPosition = me.getY();
            int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);

            // Change the color of the key pressed
            ((TextView) gridView.getItemAtPosition(position)).setBackgroundColor(Color.RED);
            return true;
    }
}

编辑:Logcat跟踪

12-10 15:34:28.081: E/InputEventReceiver(829): Exception dispatching input event.
12-10 15:34:28.081: E/MessageQueue-JNI(829): Exception in MessageQueue callback: handleReceiveCallback
12-10 15:34:28.221: E/MessageQueue-JNI(829): java.lang.ClassCastException: java.lang.String cannot be cast to android.widget.TextView
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.example.helloworld.GridViewActivity$1.onTouch(GridViewActivity.java:62)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.View.dispatchTouchEvent(View.java:7122)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.View.dispatchPointerEvent(View.java:7307)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.os.MessageQueue.nativePollOnce(Native Method)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.os.MessageQueue.next(MessageQueue.java:125)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.os.Looper.loop(Looper.java:124)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at dalvik.system.NativeStart.main(Native Method)
12-10 15:34:28.263: D/AndroidRuntime(829): Shutting down VM
12-10 15:34:28.263: W/dalvikvm(829): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-10 15:34:28.362: E/AndroidRuntime(829): FATAL EXCEPTION: main
12-10 15:34:28.362: E/AndroidRuntime(829): java.lang.ClassCastException: java.lang.String cannot be cast to android.widget.TextView
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.example.helloworld.GridViewActivity$1.onTouch(GridViewActivity.java:62)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.View.dispatchTouchEvent(View.java:7122)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.View.dispatchPointerEvent(View.java:7307)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.os.MessageQueue.nativePollOnce(Native Method)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.os.MessageQueue.next(MessageQueue.java:125)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:124)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-10 15:34:28.362: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 15:34:28.362: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:511)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-10 15:34:28.362: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

实际上,我想出来了!

我在这里使用错误的返回类型对文本视图进行类型转换

((TextView) gridView.getItemAtPosition(position)).setBackgroundColor(Color.RED);

我真正需要的是方法getChildAt而不是getItemAtPosition(返回一个字符串)

String s = (String) gridView.getItemAtPosition(position);
TextView tv = (TextView) gridView.getChildAt(position);
tv.setBackgroundColor(Color.RED);

答案 1 :(得分:0)

使用gridView.setOnItemClickListener代替。您也将获得onItemClick方法中的位置,而不必像现在那样计算它。

修改 我刚刚意识到你想要在按下项目的同时改变颜色。在这种情况下,我建议将适配器中的项目的背景设置为选择器drawable