如何让bottomSheet从顶部打开?

2024-01-07

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

我尝试删除以下行:

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

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

The view is not associated with BottomSheetBehavior

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

这是我的活动:

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


}

这是我的 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>

我不确定这是否是您想要的,但以下链接是 BottomSheetBehavior 的“顶部”版本。

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

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

包括TopSheetBehavior类到您的项目,然后您可以通过更改来使用它app:layout_behavior价值TopSheetBehavior代替android.support.design.widget.BottomSheetBehavior.

上面的代码是从原始存储库 https://github.com/TechIsFun/AndroidTopSheet,但我修复了一些库依赖项(旧的android.support图书馆->androidx库)适用于最新版本的 Android。

(也许这个答案对于@jernkuan 来说太晚了,但我希望这个答案可以对其他开发人员有所帮助。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让bottomSheet从顶部打开? 的相关文章

随机推荐