Android Design Support Library expandable Floating Action Button(FAB) menu

后端 未结 5 946
耶瑟儿~
耶瑟儿~ 2020-11-28 17:26

Now that the Android Design Support Library is out, does anyone knows how to implement expanded Fab menu with it, like the fab on Inbox App?

Should look like this:

5条回答
  •  庸人自扰
    2020-11-28 18:06

    Got a better approach to implement the animating FAB menu without using any library or to write huge xml code for animations. hope this will help in future for someone who needs a simple way to implement this.

    Just using animate().translationY() function, you can animate any view up or down just I did in my below code, check complete code in github. In case you are looking for the same code in kotlin, you can checkout the kotlin code repo Animating FAB Menu.

    first define all your FAB at same place so they overlap each other, remember on top the FAB should be that you want to click and to show other. eg:

        
    
    
    
    
    
    
    

    Now in your java class just define all your FAB and perform the click like shown below:

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab1 = (FloatingActionButton) findViewById(R.id.fab1);
        fab2 = (FloatingActionButton) findViewById(R.id.fab2);
        fab3 = (FloatingActionButton) findViewById(R.id.fab3);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!isFABOpen){
                    showFABMenu();
                }else{
                    closeFABMenu();
                }
            }
        });
    

    Use the animation().translationY() to animate your FAB,I prefer you to use the attribute of this method in DP since only using an int will effect the display compatibility with higher resolution or lower resolution. as shown below:

     private void showFABMenu(){
        isFABOpen=true;
        fab1.animate().translationY(-getResources().getDimension(R.dimen.standard_55));
        fab2.animate().translationY(-getResources().getDimension(R.dimen.standard_105));
        fab3.animate().translationY(-getResources().getDimension(R.dimen.standard_155));
    }
    
    private void closeFABMenu(){
        isFABOpen=false;
        fab1.animate().translationY(0);
        fab2.animate().translationY(0);
        fab3.animate().translationY(0);
    }
    

    Now define the above mentioned dimension inside res->values->dimens.xml as shown below:

        55dp
    105dp
    155dp
    

    That's all hope this solution will help the people in future, who are searching for simple solution.

    EDITED

    If you want to add label over the FAB then simply take a horizontal LinearLayout and put the FAB with textview as label, and animate the layouts if find any issue doing this, you can check my sample code in github, I have handelled all backward compatibility issues in that sample code. check my sample code for FABMenu in Github

    to close the FAB on Backpress, override onBackPress() as showen below:

        @Override
    public void onBackPressed() {
        if(!isFABOpen){
            this.super.onBackPressed();
        }else{
            closeFABMenu();
        }
    }
    

    The Screenshot have the title as well with the FAB,because I take it from my sample app present ingithub

提交回复
热议问题