Need to disable expand on CollapsingToolbarLayout for certain fragments

前端 未结 14 1296
萌比男神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:28

    I have used @JasonWyatt's solution and added DragCallback to behavior class to prevent touch and drag CollapsingToolbarLayout to expand it

    private void setDragCallback() {
        setDragCallback(new DragCallback() {
            @Override
            public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
                return mEnabled;
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-28 09:30

    This class will let you disable/re-enable the expansion behavior.

    public class DisableableAppBarLayoutBehavior extends AppBarLayout.Behavior {
        private boolean mEnabled;
    
        public DisableableAppBarLayoutBehavior() {
            super();
        }
    
        public DisableableAppBarLayoutBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public void setEnabled(boolean enabled) {
            mEnabled = enabled;
        }
    
        @Override
        public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
            return mEnabled && super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes);
        }
    
        public boolean isEnabled() {
            return mEnabled;
        }
    }
    

    Use it in your layout like so:

    <android.support.design.widget.AppBarLayout
        ... other attributes ...
        app:layout_behavior="com.yourpackage.DisableableAppBarLayoutBehavior"
        >
        <!-- your app bar contents -->
    </android.support.design.widget.AppBarLayout>
    

    Then, when you want to disable the behavior:

    AppBarLayout myAppBar = ....;
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) myAppBar.getLayoutParams();
    ((DisableableAppBarLayoutBehavior) layoutParams.getBehavior()).setEnabled(false);
    
    0 讨论(0)
  • 2020-11-28 09:30

    With Android Design Library v23.1.1, the method described by @LucyFair does not work. I managed to get it to work by setting the app:layout_scrollFlags to enterAlwaysCollapsed only, and the appbar stays "locked".

    Hope this helps. :)

    0 讨论(0)
  • 2020-11-28 09:32

    I found Simple solution to enable/disable collapse in CollapsingToolbarLayout:

        private void setExpandEnabled(boolean enabled) {
            mAppBarLayout.setExpanded(enabled, false);
            mAppBarLayout.setActivated(enabled);
            final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
            if (enabled)
                params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
            else
                params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
            collapsingToolbarLayout.setLayoutParams(params);
        }
    
    0 讨论(0)
  • 2020-11-28 09:32

    Find the AppBarLayout id as like this.

    appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    

    Disable expand on CollapsingToolbarLayout for certain fragments

    appBarLayout.setExpanded(true,true);
    

    Enable expand on CollapsingToolbarLayout for certain fragments

    appBarLayout.setExpanded(false,true);
    

    Hope it will help you !!

    0 讨论(0)
  • 2020-11-28 09:35

    Disable nested scrolling on the scrolling fragment content:

    recyclerView.setNestedScrollingEnabled(false);
    

    Use this if you're using the support library:

    ViewCompat.setNestedScrollingEnabled(recyclerView, false);
    
    0 讨论(0)
提交回复
热议问题