1. 程式人生 > >協調者佈局 實現上下滑動

協調者佈局 實現上下滑動

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();
    }
}