如何使底部表单从顶部打开?

7

我在我的应用程序中打开了一个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>
2个回答

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

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

完整的代码,包括示例应用程序:https://github.com/ipuris/AndroidTopSheetTopSheetBehavior类包含到您的项目中,然后通过将app:layout_behavior值更改为TopSheetBehavior而不是android.support.design.widget.BottomSheetBehavior来使用它。
上述代码是从原始存储库派生的,但我修复了一些库依赖项(旧的android.support库->最新版本的androidx库)适用于最新版本的Android。
(也许这个答案对@jernkuan来说太晚了,但我希望这个答案对其他开发者有所帮助。)

0

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