【问题标题】:How to implement Bottom Sheets using new design support library 23.2如何使用新的设计支持库 23.2 实现底部表
【发布时间】:2016-06-07 17:41:38
【问题描述】:

Google 发布了新的更新以支持库 23.2,因为他们添加了底部工作表功能。任何人都可以告诉如何使用该库实现该底部表。

【问题讨论】:

标签: android android-support-library android-support-design


【解决方案1】:

使用如下布局

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

【讨论】:

  • 如何锚定底页?
  • @KaveeshKanwal:使用 setPeekHeight 作为锚点或来自 xml app:behavior_peekHeight="100dp"
  • 问题是这只涵盖了底片设计的一部分,即持久化的部分。模态底部工作表(例如共享对话框)通常不是主布局的一部分,但有自己的。如果有人也知道如何处理该案例,那就太好了。
  • @Gábor,模态​​底页是带有自定义视图的对话框,您可以使用 BottomSheetDialog 类来显示它们。 BottomSheetDialog 类有一个方法 setContentView() 来设置自定义视图和另一个方法 show() 来显示它们。
  • @DhawalSodhaParmar 你能帮我看看底页吗? onStateChanged 如果我想在状态扩展的情况下隐藏状态,我应该怎么做?
【解决方案2】:

您可以按照此处提供的说明进行操作:http://android-developers.blogspot.com/2016/02/android-support-library-232.html

"通过将 BottomSheetBehavior 附加到 CoordinatorLayout 的子视图(即添加 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>);

【讨论】:

  • 对我来说,bottomSheetBehavior.setState(&lt;desired state&gt;); 在我的活动的onCreate()onResume 中的BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); 之后不能直接工作,它会抛出一个java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference。但是,如果我等待用户交互(单击按钮)一段时间,它就会起作用。有什么想法吗?
  • stackoverflow.com/questions/35906125/…。基本上是扩展BottomSheetBehaviour,覆盖onLayoutChild,调用super后调用setState。已提供代码。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    您可以按照 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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      相关资源
      最近更新 更多