Android ListView 不显示在 Bottom Sheet 中

标签 android listview bottom-sheet

我想创建一个导航 Bottom Sheet ,就像 Google map 导航 Bottom Sheet 中的那样,其中我们有路线方向列表。我正在使用 android 的 BottomSheetBehaviour 打开 Bottom Sheet 。我当前面临的问题是 ListView 弹出时不会显示在 Bottom Sheet 布局内。 View 只是一片空白。我还尝试在 NestedScrollView 中自行膨胀 View 以获得相同的结果,但这也没有出现。

这是我的 Bottom Sheet xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheet1"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:elevation="4dp"
android:background="@color/white"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:clipToPadding="true"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:text="This is a sheet with listview."
    android:textSize="20dp"
    android:layout_marginTop="10dp"
    android:textColor="@color/primaryText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:text="This is a secondary text!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<android.support.v4.widget.NestedScrollView
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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


    </LinearLayout>

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

<ListView
    android:id="@+id/bottomSheetListview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

在我的java代码中,

View bottomSheetView = findViewById(R.id.bottomSheet1);
ListView listView = (ListView) findViewById(R.id.bottomSheetListview);
List<String> listData = getListData(); //returns a simple array list of strings, about 15 items
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData));
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

我在这里做错了什么吗?为什么litview或scrollview不显示。

enter image description here

已编辑 我的整个 Activity

    public class BottomSheetActivity extends AppCompatActivity {

        private static final String TAG = "==> BottomSheetActivity";

        BottomSheetBehavior mBottomSheetBehavior;
        Button peek;

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

            this.peek = (Button) findViewById(R.id.peek);

            peek.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showSheet1();
                }
            });

        }

        private List<String> getListData(){
            List<String> stringList = new ArrayList<>();
            for (int i = 0; i < 15; i++) {
                stringList.add("This is string number "+i);
            }

            return stringList;
        }

        private void showSheet1(){
            if(mBottomSheetBehavior != null){
                //hide any previous bottom sheets
                mBottomSheetBehavior.setHideable(true);
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            }
            //initialize a new sheet
            View bottomSheetView = findViewById(R.id.bottomSheet1);

            ListView listView = (ListView) bottomSheetView.findViewById(R.id.bottomSheetListview);
            listView.setVisibility(View.VISIBLE);
            List<String> listData = getListData();
            listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData));

            mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }


    }

activity_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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"
    android:fitsSystemWindows="true"
    tools:context="com.vedamic.androidtutorial.BottomSheetActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_bottom_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" />

    <include layout="@layout/layout_bottom_sheet_1" />

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

content_bottom_shee.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.vedamic.androidtutorial.BottomSheetActivity"
    tools:showIn="@layout/activity_bottom_sheet">

    <Button
        android:id="@+id/peek"
        android:text="peek 1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/expand"
        android:text="peek 2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/flipboardSheet"
        android:text="Flipbard BottomSheets"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/testListView"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

最佳答案

我导入了你的xml,android studios“预览”确实显示带有android:layout_height="match_parent"NestedScrollView占据了整个空间,所以 ListView 没有剩余空间。

enter image description here

NestedScrollView 设置为 android:visibility="gone" 时,ListView 有足够的空间。

enter image description here

所以现在没有显示任何内容的唯一原因可能是因为您的 listData 为空?

编辑

好吧,我实现了所有信息,但样式非常困惑。首先,peek 1 按钮位于工具栏后面。

enter image description here

但我仍然可以点击它,所以会发生这种情况:

enter image description here

关于Android ListView 不显示在 Bottom Sheet 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910782/

相关文章:

android - ListView 基于项目的自动高度

WPF ListView : Is it possible for rows to have subrows?

javascript - 下拉菜单切换不适用于 ngTouch

android - SoftInput.AdjustResize 导致键盘在显示或隐藏时闪烁

java - 从 ArrayList<HashMap<String, String>> 获取无效值

android - 如何在Bottomsheet布局中自定义工具栏?

android - Bottomsheetbehavior 为 Bottom Sheet 的顶部增加了额外的空间

android - 是否可以在 Android 上更改 ble 设备的原始(广告)数据

android - 即使指定字段为空也处理 StringIndexOutOfBoundsException

android - java.lang.IllegalArgumentException : The view is not associated with BottomSheetBehavior 异常