Android ListView不会显示在底部表中

约格什

我想创建一个导航底表,就像Google Maps Navigational BottomSheet一样,其中我们提供了路线的方向列表。我正在使用android的BottomSheetBehaviour打开底表。我目前面临的问题是,列表视图在弹出时不会出现在底部工作表布局中。该视图只是空白。我还尝试在NestedScrollView中充气查看自己以获得相同的结果,但是也没有显示出来。

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

在此处输入图片说明

编辑了我的整个活动

    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 studio“ Preview”确实显示了NestedScrollViewwithandroid:layout_height="match_parent"占用了整个空间,因此ListView还没有剩余空间。

在此处输入图片说明

当设置NestedScrollViewandroid:visibility="gone"ListView有足够的空间。

在此处输入图片说明

因此,现在什么都没有显示的唯一原因可能是因为您listData是空的?

编辑

好的,我实现了所有信息,样式非常混乱。首先,窥视1按钮位于工具栏的后面。

在此处输入图片说明

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章