简体   繁体   中英

1 FloatingActionButton for 2 different logos

I would like to be able to change dynamically the logo of the fab "It will be my favorite button". When I click on it the logo should switch between 1 to another (like on and off).

For example when you click on it the stars becoming yellow.

在此处输入图片说明

Here is a screenshot of the simulator with my mouse on the Fab and on the right screen I'm on the onclicklistener with the 2 different drawable I want to be dynamically changeable

I believe SimpleCoder's solution is appropriate. An alternative, if you want to rely on one less boolean (but one more business rule hidden in your View), you can use the tag property of any View to store something.

Pseudo-Code:

if (mFavoriteFab.tag == null) {
    mFavoriteFab.tag = "" // no longer null
    // set drawable 1   


} else {
    mFavoriteFab.tag = null
    // set drawable 1
}

You're not saving much memory and it's not necessarily more clear (I insist this "decision" (the if ) doesn't belong in your Activity/Fragment/View code, but still, for something as simple as this, you can get away with any similar approach.

Write below outside any method.

boolean isClicked  = false;

Inside click Listener code

 {
  isClicked = !isClicked;
 if(isClicked)
    mFavoriteFab.setImageResource(R.drawable.liked);
else
    mFavoriteFab.setImageResource(R.drawable.unliked);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM