How to disable scrolling of AppBarLayout in CoordinatorLayout?

前端 未结 5 1788
挽巷
挽巷 2020-12-02 23:34

I have MapFragment with parallax effect inside AppBarLayout:

I want to disable scrolling on AppBarLayout, because it

5条回答
  •  青春惊慌失措
    2020-12-02 23:47

    You can accomplish this by defining a custom app:layout_behavior in xml. With this approach you don't have to worry about getting a reference to the LayoutParams and doing null checks.

      
    

    Then create a custom class that extends from AppBarLayout.Behavior.

    public class FixedAppBarLayoutBehavior extends AppBarLayout.Behavior {
    
      public FixedAppBarLayoutBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        setDragCallback(new DragCallback() {
          @Override public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
            return false;
          }
        });
      }
    }
    

    Updated with Kotlin version:

    class FixedAppBarLayoutBehavior(context: Context, attrs: AttributeSet) : AppBarLayout.Behavior(context, attrs) {
      init {
        setDragCallback(object : DragCallback() {
          override fun canDrag(appBarLayout: AppBarLayout): Boolean = false
        })
      }
    }
    

提交回复
热议问题