开发者

Android: Use RealViewSwitcher to switch between views like the home screen does

I am working on a small, but nice open source project: A RealViewSwitcher that allows to switch between multiple views by performing a horizontal fling - like the Launcher of Android does. It is just a simple ViewGroup that implements onInterceptTouchEvent and onTouchEvent to make things work. In fact most of the code is taken from the file Workspace.java (line 572 and following), so are the two methods to handle touch events - without many changes.

However, I don't get this thing to work. My current working class is posted here (on pastebin because it is pretty large): http://pastebin.com/8p8GCEZK

If I execute my class (just creating it, adding some TextViews and then setting it as the content view of the activity) I get the following output if I do a quick fling:

12-15 23:53:04.797: INFO/RealViewSwitcher(7477): intercept ACTION_DOWN
12-15 开发者_如何学Python23:53:04.797: INFO/RealViewSwitcher(7477): touch ACTION_DOWN
12-15 23:53:04.807: INFO/RealViewSwitcher(7477): touch ACTION_MOVE
12-15 23:53:04.840: INFO/RealViewSwitcher(7477): touch ACTION_MOVE
12-15 23:53:04.867: INFO/RealViewSwitcher(7477): touch ACTION_MOVE
12-15 23:53:04.897: INFO/RealViewSwitcher(7477): touch ACTION_MOVE
12-15 23:53:04.897: INFO/RealViewSwitcher(7477): touch ACTION_UP

My problem now is: Whatever I do, I don't get the ACTION_MOVE in my onInterceptTouchEvent method. I read the documentation multiple times and tried to play with the return values, but I don't get any other events than the ACTION_DOWN there - which is ugly, because the actual code that starts the fling resides in the ACTION_MOVE case.

Can you help me out here? The annoying thing which I can not get: It seems to be implemented the same way in the official Launcher application. But their code works, mine doesn't. What am I missing?


It's quite simple actually:

1) if you return false from onInterceptTouchEvent:

  • the current event, after you have processed it if you wish, will ALSO be dispatched to the target (child) view's onTouchEvent method;
  • the next event will still be dispatched to onInterceptTouchEvent

2) if you return true from onInterceptTouchEvent:

  • the current event will not be processed by the target view, which will receive the same event but with the action ACTION_CANCEL;
  • ALL further events will be delivered to this view's onTouchEvent method and no longer appear in onInterceptTouchEvent.

So you use option 1) if you want to process events inside onInterceptTouchEvent() and also forward them to the target view, and option 2) if you want to take over all the event flow, cutting off the target view and processing all the events inside you parent's view onTouchEvent() as if it was the original target.

I've tested option 1) and it works for me.


I had the same problem as yours. I am not sure it would work for you, but it was solved by setting the child of my custom ViewGroup to 'true'.


this is because if your children are not clickable, means they will not accept any touch event. at this time, if your viewgroup doesn't accept touch event either, android view system will not consider your viewgroup and it's decendents as touch event target, so android view system hand over touch event to the parent of your viewgroup. (nonclickable means textview will not accept touchevent)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜