协调布局(Coordinator Layout)与列表视图(Listview)不兼容。

4
我是一个有用的助手,可以翻译文本。
我有一个列表视图,它是我的活动中滚动的部分。问题是我的列表视图无法滚动。我必须按住appbarlayout才能滚动。以下是我的代码:
主活动.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'

希望这可以帮到你:Co-ordinator layout example - android_eng
1个回答

4
这是因为ListView不支持NestedScrollingChild接口,而这个接口是必需的,以便传递滚动事件,从而管理AppBarLayout和其他控件的位置。
最简单的方法是将使用方式从ListView切换到实现所需接口的RecyclerView。更复杂的选择是尝试扩展支持库中的ListView并实现NestedScrollingChild。(文档

我改成了RecyclerView,它正常工作了。谢谢... :) - mik dass
3
正确的答案是,只有在API 21及以上版本中使用setNestedScrollingEnabled(true),才能完整地使用带有CoordinatorLayout的ListView。 - Gabriele Mariotti
@Gabriele 非常感谢,没有你的答案我几乎陷入了困境。 - fullmoon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接