Android底部菜单行为不正常。视图在第一次运行时不显示。

11

您好,我有一个支持Bottom Sheet行为的应用程序。无论我尝试什么,第一次初始化显示时,Bottom Sheet都不会显示其中的元素。第二次相同的代码触发时,Bottom Sheet会显示元素。这在动态添加的视图和ListView上都发生了。以下是我为Bottom Sheet构建的xml。

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="brijhelpline.testproject.MapsActivity" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:alpha="0.9"
        android:background="@drawable/gradient">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/settings"
                android:indeterminate="false"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/userIcon"
                android:layout_width="38dp"
                android:layout_height="38dp"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="13dp"
                android:background="@drawable/user" />

            <ImageView
                android:id="@+id/settings"
                android:layout_width="38dp"
                android:layout_height="38dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="3dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/settings_icon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:text="Fuel Buddy"
                android:textColor="#ffffff"
                android:textSize="20sp" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/searc_layout">

        <ImageView
            android:id="@+id/marker"
            android:layout_width="20dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:background="@drawable/marker_icon" />

        <ImageView
            android:id="@+id/add"
            android:layout_width="25dp"
            android:layout_height="26dp"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:background="@drawable/add_icon" />

        <EditText
            android:id="@+id/inputStation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_toLeftOf="@+id/add"
            android:layout_toRightOf="@+id/marker"
            android:hint="Поиск Заправки"
            android:maxLines="1" />
    </RelativeLayout>


</RelativeLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@android:color/white"
    android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <LinearLayout
        android:id="@+id/layoutToAdd"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>
    <!--<ListView-->
    <!--android:id="@+id/listView"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"></ListView>-->
</android.support.v4.widget.NestedScrollView>

如您所见,列表视图已经被锁定了,因此我尝试了各种可能的方法,甚至用RELATIVELAYOUT或其他方式替换了NESTEDSCROLLVIEW。这里是实际将视图添加到LinearLayout的代码。

     for (int i = 0; i < loopContent; i++) {
                    String address = addresses.get(i).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                    String city = addresses.get(i).getLocality();
                    String state = addresses.get(i).getAdminArea();
                    String country = addresses.get(i).getCountryName();
                    String postalCode = addresses.get(i).getPostalCode();
                    String knownName = addresses.get(i).getFeatureName(); // Only if available else return NULL
                    if (knownName.equals("null") || knownName.toString().isEmpty()) {
                        knownName = "Нету информации";
                    }
                    if (address != null) {
                        if (address.toString().isEmpty() || address.equals("null")) {
                            address = "Нету информации";
                        }
                    } else {
                        address = "Нету информации";
                    }
//                    holder = new Holder(knownName, address);
//                    holderArrayList.add(holder);
//                    adapter.notifyDataSetChanged();

                    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
                    View inflatedLayout = inflater.inflate(R.layout.single_item, null, false);
                    TextView header = (TextView) inflatedLayout.findViewById(R.id.header);
                    TextView street = (TextView) inflatedLayout.findViewById(R.id.second);
                    header.setText(knownName);
                    street.setText(address);
                    layoutToAdd.addView(inflatedLayout);
                }
                progressBar.setVisibility(View.GONE)
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

我完全不知道问题是什么,也不知道该怎么处理它...这真的很烦人,如果有人能帮助我解决,我会非常感激。

4个回答

20

到目前为止,我发现这是Google的一个漏洞,与此同时,我找到了一个解决方法。每当您想要进行实际的展示设置(无论是折叠还是扩展),只需发布一个新的可运行项,并执行如下设置:

  bottomSheet.post(new Runnable() {
                    @Override
                    public void run() {
                        mBottomSheetBehavior.setPeekHeight(200);
                        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    }
                });

谢谢,这对我很有帮助。不知道 Google 是否会解决这个 bug。 - Cristian Olaru
经过两个小时的努力,问题终于被解决了。谢谢。 - Moulesh
很遗憾,如果我点击具有长长度的项目回收视图按钮,则无法工作。 - nicolas asinovich

2

我遇到了折叠底部面板的问题。通过设置峰值高度,我解决了这个问题。

mBottomSheetBehavior.setPeekHeight(0);

如果您希望将BottomSheet折叠并隐藏,您可以在初始化BottomSheetBehavior后添加以下代码:

1

这段代码在我的设备上运行良好,但是在一些早期版本的设备上底部工具栏无法正常工作。

  ViewCompat.postOnAnimation(coordinator, new Runnable() {
            @Override
            public void run() {
                ViewCompat.postInvalidateOnAnimation(coordinator);
            }
        });

希望这可以帮助。

是的,那个方法奏效了,令人惊讶。应该被接受为正确答案。我们知道这是为什么吗?这是否是一个 bug?谢谢! - guy_m

1
这对我有用。

而不是设置

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

做这个。
mBottomSheetBehavior.setState(mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN ? BottomSheetBehavior.STATE_COLLAPSED : BottomSheetBehavior.STATE_HIDDEN);

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