Макет координатора не работает с Listview

У меня есть список, который является частью прокрутки в моей деятельности. Проблема в том, что мой список не прокручивается. Мне нужно удерживать панель приложений для прокрутки. Вот мой код:

mainactivity.xml

<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=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/htab_collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        app:layout_collapseMode="parallax">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/image_view"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:fitsSystemWindows="true"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:layout_gravity="bottom"
            android:paddingBottom="16dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Followers"
                android:textAlignment="center"
                android:textAppearance="?android:attr/textAppearanceLarge"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Points"
                android:textAlignment="center"
                android:textAppearance="?android:attr/textAppearanceLarge"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Following"
                android:textAlignment="center"
                android:textAppearance="?android:attr/textAppearanceLarge"/>

        </LinearLayout>

    </FrameLayout>

     <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="104dp"
        android:gravity="top"
        android:minHeight="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleMarginTop="13dp" />

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

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

<ListView
    android:id="@+id/list_view"
    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="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

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

Java-код-

public class MainActivity extends AppCompatActivity { 
ListView listView;
String[] strings={"Something","Everything","Nothing","Ok","Maybe","Whatever","Does it matter","Mybe again","Nothing","Nothing","Nothing","Nothing"};
ArrayAdapter<String> adapter;
com.facebook.drawee.view.SimpleDraweeView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    image= (SimpleDraweeView)findViewById(R.id.image_view);
    image.setImageURI(Uri.parse("https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"));
    listView= (ListView) findViewById(R.id.list_view);
    adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,strings);
    listView.setAdapter(adapter);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
   }
}

Моя зависимость от градиента -

compile 'com.android.support:design:23.1.0'

person mik dass    schedule 04.11.2015    source источник
comment
Надеюсь, это поможет пример макета координатора   -  person android_eng    schedule 06.11.2015


Ответы (1)


Это связано с тем, что ListView не поддерживает интерфейс NestedScrollingChild, необходимый для доставки событий прокрутки, на которые AppBarLayout и другие полагаются для управления своим позиционированием.

Самый простой вариант — переключить использование с ListView на RecyclerView, в котором уже реализованы необходимые интерфейсы. Более сложным выбором будет попытка расширить ListView и реализовать NestedScrollingChild из библиотеки поддержки (документы).

person devunwired    schedule 04.11.2015
comment
Я изменил его на recyclerview, и это сработало. Спасибо... :) - person mik dass; 04.11.2015
comment
Правильный ответ. Для полноты вы можете использовать ListView с CoordinatorLayout только с API21+, используя setNestedScrollingEnabled(true). - person Gabriele Mariotti; 07.11.2015