RecyclerView在NestedScrollView内部时不会执行动画。

huangapple go评论85阅读模式
英文:

RecyclerView doesn't animate when it's inside a NestedScrollView

问题

我的RecyclerView在滚动时没有动画。我已经缩小了问题范围,发现问题出在RecyclerView所在的NestedScrollView上。当RecyclerViewNestedScrollView外部时,它可以正常动画,但当它在NestedScrollView内部时,就无法动画了。如何在保持RecyclerViewNestedScrollView中的情况下实现动画?

以下是相关布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/mainToolbar">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/tab_anim_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:background="@android:color/transparent"
        app:elevation="0dp">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/searchToolbar"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center|top"
            android:background="#A106A1"
            android:elevation="2dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <!-- dummy to catch focus -->
            <LinearLayout
                android:layout_width="0px"
                android:layout_height="0px"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <androidx.appcompat.widget.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="30dp"
                android:layout_marginLeft="30dp"
                android:layout_marginEnd="35dp"
                android:layout_marginRight="35dp"
                android:animateLayoutChanges="true"
                android:layoutDirection="rtl"
                app:iconifiedByDefault="true"
                app:searchHintIcon="@null" />

        </com.google.android.material.appbar.MaterialToolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingHorizontal="1dp" />

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

以下是在RecyclerView适配器中应用的动画:

@Override        
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { //binds the data to the view
    holder.container.setAnimation(AnimationUtils.loadAnimation(holder.container.getContext(), R.anim.fade_out));
    holder.nameTextView.setText(labels[position]);
    holder.packageNameTextView.setText(packageNames[position]);
    holder.iconImageView.setImageDrawable(drawables[position]);
}

更新:这里使用CollapsingToolbarLayout而不是NestedScrollView。如何使工具栏滚动?

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/mainToolbar">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/tab_anim_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:background="@android:color/transparent"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/searchToolbar"
                style="@style/Widget.MaterialComponents.Toolbar.Primary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center|top"
                android:background="#A106A1"
                android:elevation="2dp"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_scrollFlags="scroll">

                <!-- dummy to catch focus -->
                <LinearLayout
                    android:layout_width="0px"
                    android:layout_height="0px"
                    android:focusable="true"
                    android:focusableInTouchMode="true" />

                <androidx.appcompat.widget.SearchView
                    android:id="@+id/searchView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="30dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginEnd="35dp"
                    android:layout_marginRight="35dp"
                    android:animateLayoutChanges="true"
                    android:layoutDirection="rtl"
                    app:iconifiedByDefault="true"
                    app:searchHintIcon="@null" />

            </com.google.android.material.appbar.MaterialToolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="3dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
英文:

My RecyclerView doesn't animate when scrolling. I've narrowed down the problem to the NestedScrollView that the RecyclerView is in. When the RecyclerView is outside the NestedScrollView it animates fine. But when it's in the NestedScrollView it doesn't. How can I animate the RecyclerView while keeping it in the NestedScrollView?

Here's the relevant layout:

&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/mainToolbar&quot;&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:id=&quot;@+id/tab_anim_appbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center|top&quot;
android:layout_marginLeft=&quot;40dp&quot;
android:layout_marginRight=&quot;40dp&quot;
android:background=&quot;@android:color/transparent&quot;
app:elevation=&quot;0dp&quot;&gt;
&lt;com.google.android.material.appbar.MaterialToolbar
android:id=&quot;@+id/searchToolbar&quot;
style=&quot;@style/Widget.MaterialComponents.Toolbar.Primary&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center|top&quot;
android:background=&quot;#A106A1&quot;
android:elevation=&quot;2dp&quot;
app:contentInsetLeft=&quot;0dp&quot;
app:contentInsetStart=&quot;0dp&quot;
app:layout_scrollFlags=&quot;scroll|enterAlways&quot;&gt;
&lt;!-- dummy to catch focus --&gt;
&lt;LinearLayout
android:layout_width=&quot;0px&quot;
android:layout_height=&quot;0px&quot;
android:focusable=&quot;true&quot;
android:focusableInTouchMode=&quot;true&quot; /&gt;
&lt;androidx.appcompat.widget.SearchView
android:id=&quot;@+id/searchView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;30dp&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:layout_marginEnd=&quot;35dp&quot;
android:layout_marginRight=&quot;35dp&quot;
android:animateLayoutChanges=&quot;true&quot;
android:layoutDirection=&quot;rtl&quot;
app:iconifiedByDefault=&quot;true&quot;
app:searchHintIcon=&quot;@null&quot; /&gt;
&lt;/com.google.android.material.appbar.MaterialToolbar&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;androidx.core.widget.NestedScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/myRecyclerView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:paddingHorizontal=&quot;1dp&quot; /&gt;
&lt;/androidx.core.widget.NestedScrollView&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

And here's the animation applied in the RecyclerView adapter:

@Override        
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { //binds the data to the view
holder.container.setAnimation(AnimationUtils.loadAnimation(holder.container.getContext(),R.anim.fade_out));
holder.nameTextView.setText(labels[position]);
holder.packageNameTextView.setText(packageNames[position]);
holder.iconImageView.setImageDrawable(drawables[position]);
}

Update: Here it is using the CollapsingToolbarLayout instead of NestedScrollView. How do I make the toolbar scroll?

&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/mainToolbar&quot;&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:id=&quot;@+id/tab_anim_appbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center|top&quot;
android:layout_marginLeft=&quot;40dp&quot;
android:layout_marginRight=&quot;40dp&quot;
android:background=&quot;@android:color/transparent&quot;
app:elevation=&quot;0dp&quot;&gt;
&lt;com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;com.google.android.material.appbar.MaterialToolbar
android:id=&quot;@+id/searchToolbar&quot;
style=&quot;@style/Widget.MaterialComponents.Toolbar.Primary&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center|top&quot;
android:background=&quot;#A106A1&quot;
android:elevation=&quot;2dp&quot;
app:contentInsetLeft=&quot;0dp&quot;
app:contentInsetStart=&quot;0dp&quot;
app:layout_scrollFlags=&quot;scroll&quot;&gt;
&lt;!-- dummy to catch focus --&gt;
&lt;LinearLayout
android:layout_width=&quot;0px&quot;
android:layout_height=&quot;0px&quot;
android:focusable=&quot;true&quot;
android:focusableInTouchMode=&quot;true&quot; /&gt;
&lt;androidx.appcompat.widget.SearchView
android:id=&quot;@+id/searchView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;30dp&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:layout_marginEnd=&quot;35dp&quot;
android:layout_marginRight=&quot;35dp&quot;
android:animateLayoutChanges=&quot;true&quot;
android:layoutDirection=&quot;rtl&quot;
app:iconifiedByDefault=&quot;true&quot;
app:searchHintIcon=&quot;@null&quot; /&gt;
&lt;/com.google.android.material.appbar.MaterialToolbar&gt;
&lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/myRecyclerView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginTop=&quot;3dp&quot;
app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;/&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

答案1

得分: 1

这不是将RecyclerView放在NestedScrollView内的好方法。如果您想要将SearchBar放在RecyclerView上方,您可以查看此链接

您还可以查看此链接的参考资料。

英文:

It is not a good way to put RecyclerView inside NestedScrollView. If you want to make SearchBar above RecyclerView, you can check this.

You can also check this refference.

答案2

得分: 1

我检查了你的xml并进行了修改。它在我的端上工作正常。请根据你的UI要求进行修改。

N.B.:在填充数据到recyclerview之后进行检查。刚开始可能看起来不起作用,因为recyclerview上没有数据。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/tab_anim_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:fitsSystemWindows="true"
        android:background="@android:color/transparent"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar_scrolling">

            <LinearLayout
                android:layout_width="0px"
                android:layout_height="0px"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <androidx.appcompat.widget.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="30dp"
                android:layout_marginLeft="30dp"
                android:layout_marginEnd="35dp"
                android:layout_marginRight="35dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:animateLayoutChanges="true"
                android:background="@drawable/rounded_bg"
                android:layoutDirection="rtl"
                app:iconifiedByDefault="true"
                app:searchHintIcon="@null" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="3dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

在活动中:

getSupportActionBar().setBackgroundDrawable(getResources()
    .getDrawable(R.drawable.rounded_bg_action_bar));

rounded_bg_action_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/holo_purple" />

    <corners android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp" />
</shape>

rounded_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/holo_purple" />

    <corners android:bottomLeftRadius="100dp"
        android:bottomRightRadius="100dp" />
</shape>
英文:

I checked your xml and modified it. It is working on my side. Please modify it according to your UI requirements.

N.B.: Check after populating data on recyclerview. First it may seem it is not working due to no data on recyclerview.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:fitsSystemWindows=&quot;true&quot;
tools:context=&quot;.MainActivity&quot;
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:id=&quot;@+id/tab_anim_appbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center|top&quot;
android:fitsSystemWindows=&quot;true&quot;
android:background=&quot;@android:color/transparent&quot;
android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;
app:elevation=&quot;0dp&quot;&gt;
&lt;com.google.android.material.appbar.CollapsingToolbarLayout
android:id=&quot;@+id/collapsing_toolbar_layout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fitsSystemWindows=&quot;true&quot;
app:layout_scrollFlags=&quot;scroll|exitUntilCollapsed&quot;
app:toolbarId=&quot;@+id/toolbar_scrolling&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;0px&quot;
android:layout_height=&quot;0px&quot;
android:focusable=&quot;true&quot;
android:focusableInTouchMode=&quot;true&quot; /&gt;
&lt;androidx.appcompat.widget.SearchView
android:id=&quot;@+id/searchView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;30dp&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:layout_marginEnd=&quot;35dp&quot;
android:layout_marginRight=&quot;35dp&quot;
android:paddingLeft=&quot;16dp&quot;
android:paddingRight=&quot;16dp&quot;
android:animateLayoutChanges=&quot;true&quot;
android:background=&quot;@drawable/rounded_bg&quot;
android:layoutDirection=&quot;rtl&quot;
app:iconifiedByDefault=&quot;true&quot;
app:searchHintIcon=&quot;@null&quot; /&gt;
&lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/myRecyclerView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginTop=&quot;3dp&quot;
app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;/&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

In activity:

getSupportActionBar().setBackgroundDrawable(getResources()
.getDrawable(R.drawable.rounded_bg_action_bar));

rounded_bg_action_bar.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:shape=&quot;rectangle&quot;&gt;
&lt;solid android:color=&quot;@android:color/holo_purple&quot; /&gt;
&lt;corners android:bottomLeftRadius=&quot;20dp&quot;
android:bottomRightRadius=&quot;20dp&quot; /&gt;
&lt;/shape&gt;

rounded_bg.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:shape=&quot;rectangle&quot;&gt;
&lt;solid android:color=&quot;@android:color/holo_purple&quot; /&gt;
&lt;corners android:bottomLeftRadius=&quot;100dp&quot;
android:bottomRightRadius=&quot;100dp&quot; /&gt;
&lt;/shape&gt;

huangapple
  • 本文由 发表于 2020年8月5日 20:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定