Список Android не отображается на нижнем листе

Я хочу создать навигационный нижний лист, подобный тому, что в 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>

person Yogesh    schedule 07.10.2016    source источник
comment
Может быть, это потому, что ваш NestedScrollView имеет layout_height=match_parent?   -  person cherry-wave    schedule 07.10.2016
comment
Я попытался установить видимость NestedScrollView в GONE и повторно запустить приложение, все те же результаты.   -  person Yogesh    schedule 07.10.2016


Ответы (2)


Я импортировал ваш xml, и «Предварительный просмотр» студии Android действительно показал, что NestedScrollView с android:layout_height="match_parent" занимает все пространство, поэтому ListView не осталось места.

введите здесь описание изображения

При установке NestedScrollView на android:visibility="gone" в ListView достаточно места.

введите здесь описание изображения

Итак, единственная причина, по которой сейчас ничего не отображается, может быть, потому что ваш listData пуст?

ИЗМЕНИТЬ

Хорошо, я реализовал всю информацию, и стиль очень беспорядочный. Во-первых, кнопка просмотра 1 находится за панелью инструментов.

введите здесь описание изображения

Но я все еще могу щелкнуть по нему, так что это происходит:

введите здесь описание изображения

person cherry-wave    schedule 07.10.2016
comment
Спасибо за усилия, но я отладил процесс, и данные моего списка показали 15 элементов, а метод getView() адаптера был вызван 15 раз. Проблема не устранена. Есть еще идеи, как это могло происходить? - person Yogesh; 07.10.2016
comment
Не могли бы вы предоставить весь код класса Activity, куда вы добавляете нижний лист? - person cherry-wave; 07.10.2016
comment
Я импортировал вашу активность и все XML-файлы, но это выглядит совершенно беспорядочно... (см. мой обновленный ответ), но отображается список.. каким-то образом - person cherry-wave; 07.10.2016

Не делайте так, чтобы ваша видимость в списке поначалу исчезла. Нижний должен знать, какого размера он должен быть. Если вам нужно убрать его видимость, сделайте это после создания нижнего листа.

И попробуйте в своем нижнем листе content_bottom_shee.xml родительский linearlayout

android:fillViewport="true"
person Eren Utku    schedule 07.10.2016