Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with onClickListener on PhysicsLayout child view #3

Closed
edunuzzi opened this issue Aug 23, 2015 · 2 comments
Closed

Help with onClickListener on PhysicsLayout child view #3

edunuzzi opened this issue Aug 23, 2015 · 2 comments

Comments

@edunuzzi
Copy link

Hi, my name is Eduardo and i have been trying to figure out a way to set a onClickListener on a PhysicsLayout child view to change its background color. The thing is, when i do, all the physics behaviour stop working. Do you have any suggestions that could help me?

Sorry if this is a obvious question, i am just starting android developing :P

@edunuzzi edunuzzi changed the title Help with onClickListener on PhysicsLayout child Help with onClickListener on PhysicsLayout child view Aug 23, 2015
@pythoneer
Copy link

Hi,
i had the same problem. The onClickListener is obliviously interfering with the touchListener of the library, so i guess you can't have both. Fortunately you can use the touchInterface provided by the PhysicsLayout library. Just use the "touchEvents" to identify a click yourself. One way could be to track the time difference the user grabbed and released an item. If its shorter than a certain threshold its considered a click. To improve this you could also track the position of the grab and release and if it exceeds a certain distance its not a click anymore. I hope this helps.

physicsRelativeLayout.getPhysics().setOnFlingListener(new Physics.OnFlingListener() {

            private static final int clickTimeDeltaInMs = 150;
            private long grabStarTime = 0;

            @Override
            public void onGrabbed(View view) {
                grabStarTime = Calendar.getInstance().getTimeInMillis();
            }

            @Override
            public void onReleased(View view) {
                final long grabReleaseTime = Calendar.getInstance().getTimeInMillis();
                if((grabReleaseTime - grabStarTime) > clickTimeDeltaInMs) {
                    System.out.println("no click");
                    return;
                }

                 //do stuff – its a click
            }
});

@edunuzzi
Copy link
Author

edunuzzi commented Sep 7, 2015

Awesome! That worked perfectly for what i was trying to achieve! Thanks a lot man :D

@edunuzzi edunuzzi closed this as completed Sep 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants