AppBarLayout CollapsingToolbar how to disable expand on EditText click?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 12:14:09

I have find the answer. I do not know is the best solution, but it works nice. If someone has better solution, feel free to write it.

So, what i done is i put the whole AppBarLayout params to 0 when i click EditText or when EditText get focus. So, when EditText is clicked, AppBarLayout is still there but, now his height is 0 and it is not visible.

This do the trick:

 //Collaps AppBarLayout
    public void collapsAppBarLayout(){
        CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        params.height = 0; // setting new param height to 0
        //setting params to appbarLayout (now AppBarLayout is 0)
        appBarLayout.setLayoutParams(params);
        appBarLayout.setExpanded(false);
    }

Also, if you want to have expand option, you need to save height of previous params and then just add that height again.

So, this is a code how i done it: Fist, when i scroll RecycleView i disable expand of appBarLayout using addOnOffsetChangedListener. However i also save params height for future use.

int sizeParams; //save params height 


appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                    // Collapsed
                    Toast.makeText(MainActivity.this, "Collapsed", Toast.LENGTH_SHORT).show();
                    // disable expanding and scroll
                    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
                    params.setScrollFlags(0);
                    //saving height for future use
                    sizeParams =  params.height;
                } else if (verticalOffset == 0) {
                    Toast.makeText(MainActivity.this, "Extend", Toast.LENGTH_SHORT).show();
                    // Expanded
                } else {
                    // Somewhere in between
                }
            }
        });

Now i make this 2 method which i call when i need expand/collaps.

   //Collaps AppBarLayout
    public void collapsAppBarLayout(){
        CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        sizeParams =  params.height;//save params height for future use
        params.height = 0; // setting new param height to 0
        //setting params to appbarLayout (now AppBarLayout is 0
        appBarLayout.setLayoutParams(params);
        appBarLayout.setExpanded(false);
    }
    //Expand AppBarLayout
    public void expandAppBarLayout(){
        CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        params.height = 3*sizeParams; // HEIGHT
        appBarLayout.setLayoutParams(params); //add height to appbarlayout
        //enable scroll and expand 
        AppBarLayout.LayoutParams params1 = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
        params1.setScrollFlags(1);
        appBarLayout.setExpanded(true);//expand
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!