XML : Can I switch the onClick listeners?

I have been stuck on a problem with removing and re adding views for two days now, and am now thinking of a completely different process.

My original problem was:

App is responding very slowly?

but, because I never got a solution for that...I am thinking of a different way to solve the issue. Because removing and re-adding views takes so long, I am thinking of just changing the properties of the button, instead of changing the entire button itself

To swap the properties of the buttons, I will need a way to swap the onClick listeners also. Is there a way to swap the listeners?

I have one swap button. When clicked, it takes one of the other three buttons, and swaps with them. The other three buttons do nothing else.

As mentioned in the linked question, my process for swapping the buttons was the following:

  1. Remove view (button)   2. Generate random number, and from my button array list, get the      button in the array list with the index of that random number   3. Remove that random view (button)   4. Add the two buttons in each others places.    

And this would swap the buttons. Now, if I were doing the swapping properties method,

IMPORTANT: I would not be able to use the arrayList, and we would need some other way to keep track of the swap button. We cant use the arrayList because the swap button's object may have the properties of another button after being swapped.

Maybe I would need to use a mapping or something to keep track of the buttons...? How would I do that?

So, here is my original code (which does not switch properties):

    red.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(final View v) {                    // Remove the clicked button from it's frame layout parent.                      ViewManager p = (ViewManager) v.getParent();                      p.removeView(v);                      // Find another button randomly.                      Random rng = new Random();                      View v2;                      do                      {                          int i = rng.nextInt(4);                          v2 = buttons.get(i);                      }                      while (v2 == v); // Loop until you get a different button to swap with.                      // Remove the other button from it's frame layout parent.                      ViewManager p2 = (ViewManager) v2.getParent();                      p2.removeView(v2);                      // Now simply insert each button into the other buttons frame layout.                      p.addView(v2, v2.getLayoutParams()); //Adding v2, the random button, into where v, the button clicked, was before(p)!                      p2.addView(v, v.getLayoutParams()); //Adding v, the button clicked, into where v2, the random button, was before(p2)                  }     }         });    

Again, this code only removes the views...but that process is very slow.

So, I have two options:

  1. Somehow make the response faster (I don't know how...hence this)
  2. Somehow change the properties of the buttons by swapping click listeners and images. AND somehow being able to keep track of the NEW swap button, so that I can swap again.

Please guide me through one of the two options I have presented...I have been stuck on this for way to long now.

Thanks so much, I really appreciate your help!

No comments:

Post a Comment