Need to disable expand on CollapsingToolbarLayout for certain fragments

前端 未结 14 1345
萌比男神i
萌比男神i 2020-11-28 08:55

I have a AppCompatActivity that controls replacing many fragments. Here is my layout for it.

activity_main.xml



        
14条回答
  •  生来不讨喜
    2020-11-28 09:47

    All you have to do is replace CoordinatorLayout with custom implementation of CoordinatorLayout which will cheat that nested scrolling has been handled.

    MyCoordinatorLayout implementation:

    public class MyCoordinatorLayout extends CoordinatorLayout {
    
        private boolean allowForScroll = false;
    
        public MyCoordinatorLayout(Context context) {
            super(context);
        }
    
        public MyCoordinatorLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
            return allowForScroll && super.onStartNestedScroll(child, target, nestedScrollAxes);
        }
    
        public boolean isAllowForScroll() {
            return allowForScroll;
        }
    
        public void setAllowForScroll(boolean allowForScroll) {
            this.allowForScroll = allowForScroll;
        }
    }
    

    activity view xml:

    
    
        
    
            
    
            
    
            
    
                
    
                    
    
                    
    
                
    
            
    
            
    
            
    
    
            
    
            
    
        
    
    
    

    I encourage you to use custom AppBarLayout implementation with helper methods to collapse/expand toolbar. On this gist you can find one.

    Ok, now it's time to configure our toolbar in activity.

    public class ToolbarAppcompatActivity extends AppCompatActivity
            implements AppBarLayout.OnOffsetChangedListener {
    
        protected Toolbar toolbar;
        protected MyCoordinatorLayout coordinator;
        protected ControllableAppBarLayout appbar;
    
        @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            configureToolbar();
        switchFragment(new FooFragment(), "FOO", true);
        }
    
        protected void configureToolbar() {
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            coordinator = (MyCoordinatorLayout) findViewById(R.id.coordinator);
            appbar = (ControllableAppBarLayout) findViewById(R.id.appbar);
            appbar.addOnOffsetChangedListener(this);
            getDelegate().setSupportActionBar(toolbar);
        }
    
        public void switchToFragment(Fragment fragment, String tag, boolean expandToolbar){
            FragmentManager fragmentManager = getSupportFragmentManager();
    
            Fragment currentFragment = fragmentManager.findFragmentByTag(currentFragmentTag);
    
            if(currentFragment == null || !TextUtils.equals(tag, currentFragmentTag) ){
                currentFragmentTag = tag;
                fragmentManager
                        .beginTransaction()
                        .replace(R.id.flContent, fragment, currentFragmentTag)
                        .commit();
    
                if(expandToolbar){
                    expandToolbar();
                }else{
                    collapseToolbar();
                }
            }
        }
    
        protected void addFragment(Fragment fragment, String tag, boolean expandToolbar) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            currentFragmentTag = tag;
            fragmentManager
                    .beginTransaction()
                    .add(R.id.flContent, fragment, currentFragmentTag)
                    .addToBackStack(tag)
                    .commit();
    
            if(expandToolbar){
                expandToolbar();
            }else{
                collapseToolbar();
            }
        }
    
       protected void collapseToolbar(){
            appbar.collapseToolbar();
            coordinator.setAllowForScroll(false);
        }
    
        public void expandToolbar(){
            appbar.expandToolbar();
            coordinator.setAllowForScroll(true);
        }
    
    }
    

    Every time you want to switch fragment and collapse/expand toolbar just call method switchFragment/addFragment with proper boolean parameter.

    Just one last note. Make sure you use latest support libraries.

    dependencies {
    
        // android support
        compile 'com.android.support:appcompat-v7:22.2.1'
        compile 'com.android.support:recyclerview-v7:22.2.1'
        compile 'com.android.support:design:22.2.1'
    
    }
    

    Do not use include tag in AppBarLayout. It does not work

提交回复
热议问题