Ich verwende CoordinatorLayout
auf meiner Aktivitätsseite. Darunter befindet sich ListView
unter der App-Leiste. Aber es funktioniert nicht, wenn ich ListView
anstelle von NestedScrollView
verwende. Und wenn ich ListView
in NestedScrollView
stecke, wird ListView
nicht erweitert
Damit CoordinatorLayout
ordnungsgemäß funktioniert, benötigen Sie das scrollende Kind, um NestedScrollingChild zu implementieren. Solche Klassen sind NestedScrollView
und RecyclerView
.
Um es kurz zu sagen: Verwenden Sie einfach eine RecyclerView
für Ihren Bildlauf-Inhalt und es wird richtig funktionieren :)
P.S. Als Randbemerkung sehe ich keinen Grund mehr, warum Sie eine ListView
verwenden. Ich weiß, dass es eine Gewohnheit ist und es einfacher ist, es einzurichten (weil Sie es oft getan haben), aber die Verwendung einer RecyclerView
ist auf jeden Fall die empfohlene Methode.
sie können es beheben, wenn Sie addtribute
Android:fillViewport="true"
in Android.support.v4.widget.NestedScrollView
:) hinzufügen. Das ist mein Code.
<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>
ab Lollipop können Sie verwenden
setNestedScrollingEnabled(true);
in Ihrer ListView/GridView/ScrollableView. Aus der Dokumentation
Aktivieren oder deaktivieren Sie den verschachtelten Bildlauf für diese Ansicht
wenn Sie eine Abwärtskompatibilität mit älteren Betriebssystemversionen benötigen, müssen Sie die Variable RecyclerView
verwenden. Lesen Sie mehr hier
Bearbeiten. ViewCompat
hat die statische Methode setNestedScrollingEnabled(View, boolean)
. Z.B.
ViewCompat.setNestedScrollingEnabled(listView, true)
danke an @Dogcat
für den Hinweis
Fügen Sie einfach Android:fillViewport="true"
in Ihr NestedScrollView
-Tag ein
das hat bei mir funktioniert.
setze Android:fillViewport="true"
auf die NestedScrollView
fügen Sie ein Layoutelement als untergeordnetes Element zu NestedScrollView
hinzu. In meinem Fall LinearLayout
und dann Setze Android:nestedScrollingEnabled="true"
auf ListView
ListView
ein Kind von LinearLayout
Gut zu gehen
Ihre Listenansicht wird durchlaufen. Hoffe hilfe.
<?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>
Der folgende Code hat für mich funktioniert:
ViewCompat.setNestedScrollingEnabled(listView, true);
Ihre ListView
sollte innerhalb von NestedScrollView
sein
Ersetzen Sie Ihre ListView durch RecyclerView
, falls möglich, erstellen Sie Ihre customListView und setzen Sie onMeasure
von ListView
auf Folgendes:
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();
}
Diese ListView
kann nicht mehr scrollen und kann in NestedScrollView
verwendet werden.
Sie können die Listenansicht nicht innerhalb einer nestedscrollview scrollen. Verwenden Sie Recyclerview mit nestedscrollview
<?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="[email protected]" />
</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>
Fügen Sie einfach Android: nestedScrollingEnabled = "true" -Tag in Ihrer NestedScrollView hinzu.
<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>
Ich habe festgestellt, dass diese Lösung von Prokash Sarkar perfekt funktioniert, um den Inhalt einer ListView so zu erweitern, dass sie innerhalb einer NestedScrollView ordnungsgemäß scrollen kann.
Kann ListView nicht in seinem übergeordneten NestedScrollView scrollen - Android
Hoffe, das hilft anderen, die nach NestedScrollView und ListView suchen.