Coordinatorlayout 中的layout_scrollFlags在java 使用

1、进行简单的布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            tools:context=".coodinator.CoordinatorActivity6">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_scrollFlags="scroll|snap">

                <LinearLayout
                    android:id="@+id/ll_content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:nestedScrollingEnabled="true"
                    app:layout_scrollFlags="scroll|enterAlways">

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:background="@color/purple_500" />
                </LinearLayout>
            </com.google.android.material.appbar.AppBarLayout>



        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        <Button
            android:id="@+id/lock"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="lock"
            android:background="@color/black"
            />
        <Button
            android:id="@+id/unlock"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="unlock"
            android:background="@color/black"
            />
    </LinearLayout>


    <data>

    </data>
</layout>

2、在java代码进行使用

 

public class CoordinatorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator);
        AppBarLayout appBarLayout = findViewById(R.id.appBar);
        LinearLayout layout = findViewById(R.id.ll_content);
//        AppBarLayout.LayoutParams mParams = (AppBarLayout.LayoutParams) appBarLayout.getChildAt(0).getLayoutParams();
        AppBarLayout.LayoutParams mParams = (AppBarLayout.LayoutParams) layout.getLayoutParams();
        mParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
        layout.setLayoutParams(mParams);
        findViewById(R.id.unlock).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CoordinatorActivity.this,"点击了不锁定",Toast.LENGTH_SHORT).show();
                mParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL|AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
                layout.setLayoutParams(mParams);
            }
        });
        findViewById(R.id.lock).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CoordinatorActivity.this,"点击了锁定",Toast.LENGTH_SHORT).show();
                mParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
                layout.setLayoutParams(mParams);
            }
        });
    }
}

3、设置参数参考AppBarLayout.LayoutParams

 public static final int SCROLL_FLAG_NO_SCROLL = 0x0;

    /**
     * The view will be scroll in direct relation to scroll events. This flag needs to be set for
     * any of the other flags to take effect. If any sibling views before this one do not have this
     * flag, then this value has no effect.
     */
    public static final int SCROLL_FLAG_SCROLL = 0x1;

    /**
     * When exiting (scrolling off screen) the view will be scrolled until it is 'collapsed'. The
     * collapsed height is defined by the view's minimum height.
     *
     * @see ViewCompat#getMinimumHeight(View)
     * @see View#setMinimumHeight(int)
     */
    public static final int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED = 1 << 1;

    /**
     * When entering (scrolling on screen) the view will scroll on any downwards scroll event,
     * regardless of whether the scrolling view is also scrolling. This is commonly referred to as
     * the 'quick return' pattern.
     */
    public static final int SCROLL_FLAG_ENTER_ALWAYS = 1 << 2;

    /**
     * An additional flag for 'enterAlways' which modifies the returning view to only initially
     * scroll back to it's collapsed height. Once the scrolling view has reached the end of it's
     * scroll range, the remainder of this view will be scrolled into view. The collapsed height is
     * defined by the view's minimum height.
     *
     * @see ViewCompat#getMinimumHeight(View)
     * @see View#setMinimumHeight(int)
     */
    public static final int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED = 1 << 3;

    /**
     * Upon a scroll ending, if the view is only partially visible then it will be snapped and
     * scrolled to its closest edge. For example, if the view only has its bottom 25% displayed, it
     * will be scrolled off screen completely. Conversely, if its bottom 75% is visible then it will
     * be scrolled fully into view.
     */
    public static final int SCROLL_FLAG_SNAP = 1 << 4;

    /**
     * An additional flag to be used with 'snap'. If set, the view will be snapped to its top and
     * bottom margins, as opposed to the edges of the view itself.
     */
    public static final int SCROLL_FLAG_SNAP_MARGINS = 1 << 5;

参考: Toolbar的 app:layout_scrollFlags属性可以在java里设定吗? - 知乎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值