如何使用新的设计支持库 23.2 来实现底部工作表

32

谷歌发布了新的支持库23.2版本,其中增加了底部工作表功能。有人能告诉我如何使用该库实现底部工作表吗?


http://android-developers.blogspot.in/2016/02/android-support-library-232.html - Ankit Aggarwal
4个回答

37

在此输入图片描述

在此输入图片描述

按照以下布局使用。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout>

            <ImageView/>

            <android.support.v7.widget.Toolbar/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout>

            //.....

        </LinearLayout>


    </android.support.v4.widget.NestedScrollView>

    <FrameLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_hideable="true"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        //your bottom sheet layout

        </LinearLayout>
    </FrameLayout>


    <android.support.design.widget.FloatingActionButton/>

</android.support.design.widget.CoordinatorLayout>
在活动中。
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);
// The View with the BottomSheetBehavior
View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        // React to state change
        Log.e("onStateChanged", "onStateChanged:" + newState);
        if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                fab.setVisibility(View.GONE);
            } else {
                fab.setVisibility(View.VISIBLE);
            }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        // React to dragging events
        Log.e("onSlide", "onSlide");
    }
});

behavior.setPeekHeight(100);

如何将底部表格固定? - Kaveesh Kanwal
2
@KaveeshKanwal:使用setPeekHeight来设置锚点,或者从xml中使用app:behavior_peekHeight="100dp"。 - Dhaval Parmar
问题在于,这只覆盖了底部表单设计的一部分,即持久性表单。模态底部表单(例如共享对话框)通常不是主要布局的一部分,而是有自己的布局。如果有人有关于如何处理这种情况的想法,那就太好了。 - Gábor
@Gábor,Modal底部表是带有自定义视图的对话框,您可以使用BottomSheetDialog类来显示它们。 BottomSheetDialog类具有一个setContentView()方法来设置自定义视图和另一个show()方法来显示它们。 - mradzinski
@DhawalSodhaParmar,你能帮我解决底部表单的问题吗?如果我想在状态扩展时隐藏状态,我应该在onStateChanged中做什么? - Jay Rathod
@jaydroider:检查一下这个答案,它可能对你有帮助 https://dev59.com/nVsV5IYBdhLWcg3wyxV_#35794743 - Dhaval Parmar

9
您可以按照此处提供的说明进行操作:http://android-developers.blogspot.com/2016/02/android-support-library-232.html “通过将BottomSheetBehavior附加到CoordinatorLayout的子View(即添加app:layout_behavior=”android.support.design.widget.BottomSheetBehavior”),您将自动获得适当的触摸检测功能,以在五个状态之间进行过渡…”
<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white">

    <!-- Your Widgets -->

    <FrameLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        app:behavior_hideable="true"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test" />

    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>

那么根据您的活动:

View bottomSheet = findViewById(R.id.bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setState(<desired state>);

9
еңЁжҲ‘зҡ„жҙ»еҠЁзҡ„onCreate()жҲ–иҖ…onResume()ж–№жі•дёӯпјҢеҪ“жҲ‘дҪҝз”ЁBottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);д№ӢеҗҺзӣҙжҺҘдҪҝз”ЁbottomSheetBehavior.setState(<desired state>);дёҚиө·дҪңз”ЁпјҢдјҡжҠӣеҮәдёҖдёӘjava.lang.Object java.lang.ref.WeakReference.get()' on a null object referenceзҡ„й”ҷиҜҜгҖӮдҪҶжҳҜеҰӮжһңзӯүеҫ…дёҖдәӣж—¶й—ҙи®©з”ЁжҲ·иҝӣиЎҢдёҖдәӣдәӨдә’пјҲдҫӢеҰӮжҢүй’®зӮ№еҮ»пјүпјҢе®ғе°ұдјҡиө·дҪңз”ЁгҖӮжңүд»Җд№Ҳжғіжі•еҗ—пјҹ - tufekoi
请参见https://dev59.com/FlsV5IYBdhLWcg3wsQrf#36001158。 基本上是扩展BottomSheetBehaviour,重写onLayoutChild并在调用super之后调用setState。代码已经提供。 - McP

6

Gradle: 第一次使用 compile 'com.android.support:design:23.2.0'

在您的布局中

<include layout="@layout/content_sheet" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bottom_sheet"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    style="@style/Widget.Design.BottomSheet.Modal">
    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="match_parent"></CalendarView>
</FrameLayout>

在Java中

    CoordinatorLayout coordinatorLayout= (CoordinatorLayout) findViewById(R.id.cl_main);
    final View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
    final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {

        }
        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // React to dragging events

        }
    });
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED );
        }
    });

1

您可以在tutsplus.com上查看教程 https://code.tutsplus.com/articles/how-to-use-bottom-sheets-with-the-design-support-library--cms-26031

<android.support.design.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <com.google.android.gms.maps.MapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:uiCompass="true"
        app:uiMapToolbar="false" />


     <android.support.v4.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            app:behavior_peekHeight="100dp"
            app:behavior_hideable="true"
            android:layout_height="350dp"
            android:clipToPadding="true"
            android:background="@android:color/holo_orange_light"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            >
     
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/ipsum"
                android:padding="16dp"
                android:textSize="16sp"/>
     
        </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

如果您希望它可以滑动消失,请确保在标签上添加app:behavior_hideable="true"
在您的活动中,您可以这样说:
  BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet));
  mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

  findViewById(R.id.button).setOnClickListener(p-> mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接