协调器布局不适用于 Listview

Coordiantor Layout not working with Listview

我有一个列表视图,它是我 activity 中的滚动部分。问题是我的列表视图没有滚动。我必须将 appbarlayout 保持到 scroll.Here 是我的代码-

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();
        }
    });
   }
}

我的gradle依赖-

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

这是因为ListView不支持NestedScrollingChild接口,而NestedScrollingChild接口需要传递AppBarLayout和其他人用来管理其定位的滚动事件。

最简单的选择是将您的用法从 ListView 切换到 RecyclerView——它已经实现了必要的接口。一个更复杂的选择是尝试扩展 ListView 并从支持库 (docs) 实现 NestedScrollingChild