mercredi 12 avril 2017

Implement reusable custom on click listener for custom view

I have a custom view (button) when I click on it I want to do some animation before.

mycustomButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {   
           //FIRST
           startAnimation()

           //THEN
           //... call what you want ...
        }
});

This code works BUT I want to use the same custom view for many app and I don't want to copy the same code for each app so I create a module(contains):

  • MyCustomView(Button)
  • CustomAnimation
  • ...

So I want to use my module for more than one app. And at each time I do the same thing when I click on my custom view:

  • First, start animation (the same animation for all app).
  • Then, call want I want

How I can implement a custom on click listener when I click on it does an animation before and I want to keep the default on click parameter (View view)

Something like that:

mycustomview.setCustomOnClickListener(new MyCustomView.OnClickListener() {
    @Override
    public void onClick(View view) {   
        //... call what you want ...
    }
});

Examples:

App 1:

mycustomview.setCustomOnClickListener(new MyCustomView.OnClickListener() {
    @Override
    public void onClick(View view) {   
        callCalenderActivity();
    }
});

App 2:

mycustomview.setCustomOnClickListener(new MyCustomView.OnClickListener() {
    @Override
    public void onClick(View view) {  
        compute(); 
        callOpenGLActivity();
    }
});

Aucun commentaire:

Enregistrer un commentaire