我在我的活动页面中使用CoordinatorLayout
。 在应用栏下方有一个ListView
。 但是,当我使用ListView
而不是NestedScrollView
时,它无法正常工作。如果我将ListView
放入NestedScrollView
中,则ListView
不会扩展。
我在我的活动页面中使用CoordinatorLayout
。 在应用栏下方有一个ListView
。 但是,当我使用ListView
而不是NestedScrollView
时,它无法正常工作。如果我将ListView
放入NestedScrollView
中,则ListView
不会扩展。
当你添加android.support.v4.widget.NestedScrollView
中的android:fillViewport="true"
属性时,你就可以修复它了 :) 。这是我的代码。
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true"
>
<ListView
android:id="@+id/list_myContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
>
</ListView>
</android.support.v4.widget.NestedScrollView>
从棒棒糖版本开始,您可以使用
setNestedScrollingEnabled(true);
在您的ListView/GridView/ScrollableView上启用或禁用嵌套滚动。来自文档
为此视图启用或禁用嵌套滚动
如果您需要与旧版操作系统向后兼容,则必须使用RecyclerView
。您可以在此处了解更多信息。
编辑。
ViewCompat
具有静态方法setNestedScrollingEnabled(View,boolean)
。例如:
ViewCompat.setNestedScrollingEnabled(listView, true)
感谢@Dogcat
指出此问题。
RecyclerView
,因为我只阅读了被接受的答案。是的,当然我可以阅读所有的答案,但第一个对我有效 - 只是更改实现方式有点麻烦 ;-) - DarwindCoordinatorLayout
正常工作,您需要使滚动子视图实现NestedScrollingChild。这种类包括NestedScrollView
和RecyclerView
。RecyclerView
作为滚动内容,它就会正确地工作 :)ListView
了。我知道这是一种习惯,并且设置起来更加容易(因为您已经做过很多次),但是使用RecyclerView
是推荐的方式。这是对我有用的方法。
在 NestedScrollView
上设置 android:fillViewport="true"
将一个布局元素作为 NestedScrollView
的子元素添加。在我的情况下是 LinearLayout
,然后在 ListView
上设置 android:nestedScrollingEnabled="true"
,让 ListView
成为 LinearLayout
的子元素。
就可以了
NestedScrollView
标签中添加android:fillViewport="true"
即可。你的列表视图将会滚动。希望有所帮助。
<?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.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true">
</ListView>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Below code worked for me:
ViewCompat.setNestedScrollingEnabled(listView, true);
您的ListView
应该在NestedScrollView
中
RecyclerView
替换你的ListView,否则创建自定义ListView并将ListView
的onMeasure
设置为以下内容: public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
这个 ListView
将不再能够滚动,可以在 NestedScrollView
中使用。
在嵌套滚动视图中无法滚动列表视图。请使用带有嵌套滚动视图的RecyclerView。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="76dp"
android:layout_height="76dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:src="@drawable/profile"
app:border_color="#FF000000" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/profile_image"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IRFAN QURESHI"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="irfan123@gmail.com" />
</LinearLayout>
<ImageView
android:layout_marginLeft="50dp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_delete_black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"
android:gravity="center_horizontal"
android:padding="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/login_email_bg_round_rect_shape"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="POST A QUERY" />
</LinearLayout>
<!--<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<RelativeLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="8dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIGN OUT" />
<ImageView
android:paddingTop="5dp"
android:layout_marginRight="40dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_delete_black" />
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:nestedScrollingEnabled="true">
<ListView
android:id="@+id/list_myContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</ListView>