Ich habe ein Layout, das eine CardView
und eine FloatingActionButton
zugeordnet ist. Es gibt eine Liste von Antworten unter der CardView
(die eine RecyclerView
ist). Manchmal ist die CardViews'
Höhe größer als der Bildschirm, also habe ich layout_height="wrap_content"
für die CardView
verwendet und das ganze LinearLayout
in eine ScrollView
eingewickelt.RecyclerView in einem ScrollView/NestedScrollView scrollt nicht ordnungsgemäß
Dies verursacht jedoch ein Problem (seit es eine Scroll-Ansicht in einem ScrollView
) beim Scrollen der Elemente der RecyclerView
. Wie in einigen der questions und answers posted, habe ich sowohl die NestedScrollView
und die android:nestedScrollingEnabled="true"
-Tag verwendet, aber das Scrollen in der RecyclerView
ist immer noch schlecht.
Hier ist meine Layout
Datei -
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.forum.reply.ReplyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/reply_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:titleTextColor="@android:color/white"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/topic_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/card_margin"
android:paddingLeft="@dimen/card_margin"
android:paddingRight="@dimen/card_margin"
android:paddingTop="@dimen/card_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingEnd="@dimen/card_margin"
android:paddingStart="@dimen/card_margin">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/topic_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/topic_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<ProgressBar
android:id="@+id/reply_progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
android:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list_of_replies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/reply_to_topic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_reply_white_24dp"
app:layout_anchor="@id/topic_card"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
Hier sind einige Bilder -
Können Sie erklären, warum das funktioniert? Außerdem muss ich Geräte unterstützen, die älter sind als API v21. –
Sie haben mehrere Scroll-Ansichten in Ihrem Layout. Wenn Sie also scrollen, während Sie Ihre RecyclerView berühren, scrollt die RecyclerView mit der übergeordneten Scrollansicht. Dies verursacht Jitter in RecyclerView, also mit NestedScrollingEnabled = "false" stoppen Sie den Scroll von recyclerView und der einzige Scroll, der ausgelöst wird, stammt von der übergeordneten ScrollView. Auch ich programmiere das programmatisch mit .setNestScrollingEnabled() und funktioniert für mich auf einem Gerät, das älter ist als API v21. –
Ich habe die Antwort akzeptiert, aber könntest du sie mit den Dingen aktualisieren, die du im Kommentar erwähnt hast. –