协调者布局 实现上下滑动

来源:互联网 发布:linux查看版本命令 编辑:程序博客网 时间:2024/06/11 17:01

activity_main

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:fitsSystemWindows="true"    tools:context="com.tools.ceshi4.MainActivity">    <Button        android:id="@+id/bt_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="按钮" />    <include layout="@layout/layout_bottom"/></android.support.design.widget.CoordinatorLayout>

layout_bottom

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/bottom_sheet"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/black"    android:orientation="vertical"    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">    <LinearLayout        android:layout_width="match_parent"        android:orientation="vertical"        android:layout_height="800dp"        android:background="@android:color/holo_red_dark">        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="按钮" />    </LinearLayout></android.support.v4.widget.NestedScrollView>
代码

package com.tools.ceshi4;import android.os.Bundle;import android.support.design.widget.BottomSheetBehavior;import android.support.v4.widget.NestedScrollView;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import butterknife.Bind;import butterknife.ButterKnife;import butterknife.OnClick;public class MainActivity extends AppCompatActivity {    @Bind(R.id.bt_1)    Button bt1;    @Bind(R.id.bottom_sheet)    NestedScrollView bottomSheet;    private BottomSheetBehavior mBottomSheetBehavior;    private View mBottomSheet;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        mBottomSheet = findViewById(R.id.bottom_sheet);        mBottomSheetBehavior = BottomSheetBehavior.from(mBottomSheet);        mBottomSheetBehavior.setPeekHeight(450);        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);    }    private void collapsed() {        mBottomSheetBehavior.setPeekHeight(450);        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);    }    @OnClick(R.id.bt_1)    public void onViewClicked() {        collapsed();    }}



原创粉丝点击