Menu item on bottom of CollapsingToolbarLayout when expanded

前端 未结 3 598
醉酒成梦
醉酒成梦 2020-12-14 12:58

I\'ve been searching for a while as shown on the following images but unfortunately I was not able to find anything similar

edit menu item is moving to

3条回答
  •  暖寄归人
    2020-12-14 13:46

    In XML layout:-

    
    
        
    
            
    
    
                    
    
                
    
                
    
                
    
            
        
    
        
    
        
    

    Menu item XML:-

        
    
    
        
    
    

    In activity:-

    public class ScrollingActivity extends AppCompatActivity {
    
    private AppBarLayout app_bar;
    private boolean appBarExpanded;
    private Menu itemMenu;
    private CollapsingToolbarLayout toolbar_layout;
    private ImageView img_edit;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar =  findViewById(R.id.toolbar);
        app_bar =  findViewById(R.id.app_bar);
        toolbar_layout =  findViewById(R.id.toolbar_layout);
        img_edit =  findViewById(R.id.img_edit);
        setSupportActionBar(toolbar);
    
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    
        app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //  Vertical offset == 0 indicates appBar is fully expanded.
                if (Math.abs(verticalOffset) > 200) {
                    appBarExpanded = false;
                    img_edit.setVisibility(View.GONE);
                    invalidateOptionsMenu();
                } else {
                    img_edit.setVisibility(View.VISIBLE);
                    appBarExpanded = true;
                    invalidateOptionsMenu();
                }
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_scrolling,menu);
        itemMenu = menu;
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (itemMenu != null
                && (!appBarExpanded || itemMenu.size() != 1)) {
            //collapsed
            itemMenu.add("Edit")
                    .setIcon(android.R.drawable.ic_menu_edit)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        } else {
            //expanded
    
        }
        return super.onPrepareOptionsMenu(itemMenu);
    }
    

    }

    I have not added subtitle with the toolbar. If you need subtitle you have to use some custom view class or library.

提交回复
热议问题