如何让bottomSheet从顶部打开? [英] how to get bottomSheet to open from the top?

查看:19
本文介绍了如何让bottomSheet从顶部打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中打开一个 NestedScrollView,到目前为止它是从屏幕底部打开的.如何从顶部打开它?

我尝试删除以下行:

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

但应用程序崩溃并出现以下错误:

该视图与BottomSheetBehavior没有关联

有没有办法从屏幕顶部打开底部工作表?

这是我的活动:

ViewPager mainViewPager;私有BottomSheetBehavior mBottomSheetBehavior;int 切换器 = 1;视图菜单弹出视图;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.workorder_selection_layout_with_fragment);工具栏工具栏 = (工具栏) findViewById(R.id.toolbar);setSupportActionBar(工具栏);..........过滤器按钮 =(按钮)findViewById(R.id.filterButtonMainWorkorderSelection);filterButton.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {如果(切换器 == 1){mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);切换器 = 2;} 别的 {mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);切换器 = 1;}}});/////////////////////////////////////////////////////////屁股表/////////////////////////////////////////////////////////////查看bottomSheet = findViewById(R.id.bottom_sheet);mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);}

这是我的 xml:

<android.support.design.widget.AppBarLayoutandroid:id="@+id/appbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingTop="8dp"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/工具栏"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:layout_scrollFlags="scroll|enterAlways"app:popupTheme="@style/AppTheme.PopupOverlay"></android.support.v7.widget.Toolbar><android.support.design.widget.TabLayoutandroid:id="@+id/tabsLayoutWorkorderSelection"android:layout_width="match_parent"android:layout_height="wrap_content"/></android.support.design.widget.AppBarLayout><android.support.v4.view.ViewPagerandroid:id="@+id/workorderSelectionMainViewPagerContainer"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"/><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="结束|底部"android:src="@drawable/add_workorders_plus"android:layout_marginLeft="@dimen/fab_margin"android:layout_marginRight="@dimen/fab_margin"android:layout_marginBottom="@dimen/fab_margin"android:layout_alignParentBottom="true"android:layout_alignParentEnd="true"/><android.support.design.widget.CoordinatorLayoutxmlns: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.v4.widget.NestedScrollViewandroid:id="@+id/bottom_sheet"android:layout_width="match_parent"android:layout_height="wrap_content"android:clipToPadding="true"机器人:背景=#293339"app:layout_behavior="android.support.design.widget.BottomSheetBehavior"><相对布局android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:layout_centerVertical =真"android:layout_toStartOf="@+id/fab">.........<查看android:layout_width="match_parent"android:layout_height="20dp"android:layout_below="@+id/workorderFilterPopup_CompleteImage"></查看></RelativeLayout></android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinatorLayout></RelativeLayout>

解决方案

我不确定这是您想要的,但以下链接是 BottomSheetBehavior 的顶级"版本.

https://github.com/ipuris/AndroidTopSheet/blob/master/library/src/main/java/com/github/techisfun/android/topsheet/TopSheetBehavior.java

包括示例应用的完整代码:https://github.com/ipuris/AndroidTopSheet

TopSheetBehavior 类包含到您的项目中,然后您可以通过将 app:layout_behavior 值更改为 TopSheetBehavior 而不是 来使用它>android.support.design.widget.BottomSheetBehavior.

以上代码分叉自 原始存储库,但我修复了一些库依赖项(旧 android.support library -> androidx library) 用于最新版本的 Android.

(也许这个答案对@jernkuan 来说太晚了,但我希望这个答案可以帮助其他开发者.)

I'm opening a NestedScrollView in my app and so far it opens from the bottom of the screen. How do I make it open from the top?

I tried removing the following line:

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

But the app crashes with the following error:

The view is not associated with BottomSheetBehavior

Is there a way to open the bottom sheet from the top of the screen?

Here is my activity:

ViewPager mainViewPager;
private BottomSheetBehavior mBottomSheetBehavior;
int switcher = 1;
View menuPopupView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workorder_selection_layout_with_fragment);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ...
    ....
    ...


    filterButton = (Button)       
    findViewById(R.id.filterButtonMainWorkorderSelection);
    filterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (switcher == 1) {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                switcher = 2;
            } else {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                switcher = 1;
            }


        }
    });

    ///////////////////////////////////////////
    //////////////Buttom Sheet/////////////////
    ///////////////////////////////////////////

    View bottomSheet = findViewById(R.id.bottom_sheet);

    mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);


}

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="apps.radwin.wintouch.activities.alignmentActivities.WorkordersSelectionActivity"
    tools:showIn="@layout/app_bar_workorders_selection">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabsLayoutWorkorderSelection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/workorderSelectionMainViewPagerContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/add_workorders_plus"
        android:layout_marginLeft="@dimen/fab_margin"
        android:layout_marginRight="@dimen/fab_margin"
        android:layout_marginBottom="@dimen/fab_margin"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />


    <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.v4.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="true"
            android:background="#293339"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toStartOf="@+id/fab">


                ...
                ...
                ...


                <View
                    android:layout_width="match_parent"
                    android:layout_height="20dp"
                    android:layout_below="@+id/workorderFilterPopup_CompleteImage"
                    ></View>


            </RelativeLayout>



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

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




</RelativeLayout>

解决方案

I'm not sure this is what you want, but the following link is a 'top' version of BottomSheetBehavior.

https://github.com/ipuris/AndroidTopSheet/blob/master/library/src/main/java/com/github/techisfun/android/topsheet/TopSheetBehavior.java

Full code including example app: https://github.com/ipuris/AndroidTopSheet

Include the TopSheetBehavior class to your project, then you can use it by changing the app:layout_behavior value to TopSheetBehavior instead of android.support.design.widget.BottomSheetBehavior.

Above code is forked from the original repository, but I fixed some library dependencies (old android.support library -> androidx library) for latest version of Android.

(Maybe this answer is too late for @jernkuan, but I hope this answer can be helpful other developers.)

这篇关于如何让bottomSheet从顶部打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆