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

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

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

问题

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

以下是相关布局:

  1. <androidx.coordinatorlayout.widget.CoordinatorLayout
  2. android:layout_width="0dp"
  3. android:layout_height="0dp"
  4. app:layout_constraintBottom_toBottomOf="parent"
  5. app:layout_constraintEnd_toEndOf="parent"
  6. app:layout_constraintStart_toStartOf="parent"
  7. app:layout_constraintTop_toBottomOf="@+id/mainToolbar">
  8. <com.google.android.material.appbar.AppBarLayout
  9. android:id="@+id/tab_anim_appbar"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center|top"
  13. android:layout_marginLeft="40dp"
  14. android:layout_marginRight="40dp"
  15. android:background="@android:color/transparent"
  16. app:elevation="0dp">
  17. <com.google.android.material.appbar.MaterialToolbar
  18. android:id="@+id/searchToolbar"
  19. style="@style/Widget.MaterialComponents.Toolbar.Primary"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_gravity="center|top"
  23. android:background="#A106A1"
  24. android:elevation="2dp"
  25. app:contentInsetLeft="0dp"
  26. app:contentInsetStart="0dp"
  27. app:layout_scrollFlags="scroll|enterAlways">
  28. <!-- dummy to catch focus -->
  29. <LinearLayout
  30. android:layout_width="0px"
  31. android:layout_height="0px"
  32. android:focusable="true"
  33. android:focusableInTouchMode="true" />
  34. <androidx.appcompat.widget.SearchView
  35. android:id="@+id/searchView"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:layout_marginStart="30dp"
  39. android:layout_marginLeft="30dp"
  40. android:layout_marginEnd="35dp"
  41. android:layout_marginRight="35dp"
  42. android:animateLayoutChanges="true"
  43. android:layoutDirection="rtl"
  44. app:iconifiedByDefault="true"
  45. app:searchHintIcon="@null" />
  46. </com.google.android.material.appbar.MaterialToolbar>
  47. </com.google.android.material.appbar.AppBarLayout>
  48. <androidx.core.widget.NestedScrollView
  49. android:layout_width="match_parent"
  50. android:layout_height="match_parent"
  51. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  52. <androidx.recyclerview.widget.RecyclerView
  53. android:id="@+id/myRecyclerView"
  54. android:layout_width="match_parent"
  55. android:layout_height="match_parent"
  56. android:paddingHorizontal="1dp" />
  57. </androidx.core.widget.NestedScrollView>
  58. </androidx.coordinatorlayout.widget.CoordinatorLayout>

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

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

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

  1. <androidx.coordinatorlayout.widget.CoordinatorLayout
  2. android:layout_width="0dp"
  3. android:layout_height="0dp"
  4. app:layout_constraintBottom_toBottomOf="parent"
  5. app:layout_constraintEnd_toEndOf="parent"
  6. app:layout_constraintStart_toStartOf="parent"
  7. app:layout_constraintTop_toBottomOf="@+id/mainToolbar">
  8. <com.google.android.material.appbar.AppBarLayout
  9. android:id="@+id/tab_anim_appbar"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center|top"
  13. android:layout_marginLeft="40dp"
  14. android:layout_marginRight="40dp"
  15. android:background="@android:color/transparent"
  16. app:elevation="0dp">
  17. <com.google.android.material.appbar.CollapsingToolbarLayout
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content">
  20. <com.google.android.material.appbar.MaterialToolbar
  21. android:id="@+id/searchToolbar"
  22. style="@style/Widget.MaterialComponents.Toolbar.Primary"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:layout_gravity="center|top"
  26. android:background="#A106A1"
  27. android:elevation="2dp"
  28. app:contentInsetLeft="0dp"
  29. app:contentInsetStart="0dp"
  30. app:layout_scrollFlags="scroll">
  31. <!-- dummy to catch focus -->
  32. <LinearLayout
  33. android:layout_width="0px"
  34. android:layout_height="0px"
  35. android:focusable="true"
  36. android:focusableInTouchMode="true" />
  37. <androidx.appcompat.widget.SearchView
  38. android:id="@+id/searchView"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:layout_marginStart="30dp"
  42. android:layout_marginLeft="30dp"
  43. android:layout_marginEnd="35dp"
  44. android:layout_marginRight="35dp"
  45. android:animateLayoutChanges="true"
  46. android:layoutDirection="rtl"
  47. app:iconifiedByDefault="true"
  48. app:searchHintIcon="@null" />
  49. </com.google.android.material.appbar.MaterialToolbar>
  50. </com.google.android.material.appbar.CollapsingToolbarLayout>
  51. </com.google.android.material.appbar.AppBarLayout>
  52. <androidx.recyclerview.widget.RecyclerView
  53. android:id="@+id/myRecyclerView"
  54. android:layout_width="match_parent"
  55. android:layout_height="match_parent"
  56. android:layout_marginTop="3dp"
  57. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  58. </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:

  1. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
  2. android:layout_width=&quot;0dp&quot;
  3. android:layout_height=&quot;0dp&quot;
  4. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  5. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  6. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  7. app:layout_constraintTop_toBottomOf=&quot;@+id/mainToolbar&quot;&gt;
  8. &lt;com.google.android.material.appbar.AppBarLayout
  9. android:id=&quot;@+id/tab_anim_appbar&quot;
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:layout_gravity=&quot;center|top&quot;
  13. android:layout_marginLeft=&quot;40dp&quot;
  14. android:layout_marginRight=&quot;40dp&quot;
  15. android:background=&quot;@android:color/transparent&quot;
  16. app:elevation=&quot;0dp&quot;&gt;
  17. &lt;com.google.android.material.appbar.MaterialToolbar
  18. android:id=&quot;@+id/searchToolbar&quot;
  19. style=&quot;@style/Widget.MaterialComponents.Toolbar.Primary&quot;
  20. android:layout_width=&quot;match_parent&quot;
  21. android:layout_height=&quot;wrap_content&quot;
  22. android:layout_gravity=&quot;center|top&quot;
  23. android:background=&quot;#A106A1&quot;
  24. android:elevation=&quot;2dp&quot;
  25. app:contentInsetLeft=&quot;0dp&quot;
  26. app:contentInsetStart=&quot;0dp&quot;
  27. app:layout_scrollFlags=&quot;scroll|enterAlways&quot;&gt;
  28. &lt;!-- dummy to catch focus --&gt;
  29. &lt;LinearLayout
  30. android:layout_width=&quot;0px&quot;
  31. android:layout_height=&quot;0px&quot;
  32. android:focusable=&quot;true&quot;
  33. android:focusableInTouchMode=&quot;true&quot; /&gt;
  34. &lt;androidx.appcompat.widget.SearchView
  35. android:id=&quot;@+id/searchView&quot;
  36. android:layout_width=&quot;match_parent&quot;
  37. android:layout_height=&quot;wrap_content&quot;
  38. android:layout_marginStart=&quot;30dp&quot;
  39. android:layout_marginLeft=&quot;30dp&quot;
  40. android:layout_marginEnd=&quot;35dp&quot;
  41. android:layout_marginRight=&quot;35dp&quot;
  42. android:animateLayoutChanges=&quot;true&quot;
  43. android:layoutDirection=&quot;rtl&quot;
  44. app:iconifiedByDefault=&quot;true&quot;
  45. app:searchHintIcon=&quot;@null&quot; /&gt;
  46. &lt;/com.google.android.material.appbar.MaterialToolbar&gt;
  47. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  48. &lt;androidx.core.widget.NestedScrollView
  49. android:layout_width=&quot;match_parent&quot;
  50. android:layout_height=&quot;match_parent&quot;
  51. app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;&gt;
  52. &lt;androidx.recyclerview.widget.RecyclerView
  53. android:id=&quot;@+id/myRecyclerView&quot;
  54. android:layout_width=&quot;match_parent&quot;
  55. android:layout_height=&quot;match_parent&quot;
  56. android:paddingHorizontal=&quot;1dp&quot; /&gt;
  57. &lt;/androidx.core.widget.NestedScrollView&gt;
  58. &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

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

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

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

  1. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout
  2. android:layout_width=&quot;0dp&quot;
  3. android:layout_height=&quot;0dp&quot;
  4. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  5. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  6. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  7. app:layout_constraintTop_toBottomOf=&quot;@+id/mainToolbar&quot;&gt;
  8. &lt;com.google.android.material.appbar.AppBarLayout
  9. android:id=&quot;@+id/tab_anim_appbar&quot;
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:layout_gravity=&quot;center|top&quot;
  13. android:layout_marginLeft=&quot;40dp&quot;
  14. android:layout_marginRight=&quot;40dp&quot;
  15. android:background=&quot;@android:color/transparent&quot;
  16. app:elevation=&quot;0dp&quot;&gt;
  17. &lt;com.google.android.material.appbar.CollapsingToolbarLayout
  18. android:layout_width=&quot;wrap_content&quot;
  19. android:layout_height=&quot;wrap_content&quot;&gt;
  20. &lt;com.google.android.material.appbar.MaterialToolbar
  21. android:id=&quot;@+id/searchToolbar&quot;
  22. style=&quot;@style/Widget.MaterialComponents.Toolbar.Primary&quot;
  23. android:layout_width=&quot;match_parent&quot;
  24. android:layout_height=&quot;wrap_content&quot;
  25. android:layout_gravity=&quot;center|top&quot;
  26. android:background=&quot;#A106A1&quot;
  27. android:elevation=&quot;2dp&quot;
  28. app:contentInsetLeft=&quot;0dp&quot;
  29. app:contentInsetStart=&quot;0dp&quot;
  30. app:layout_scrollFlags=&quot;scroll&quot;&gt;
  31. &lt;!-- dummy to catch focus --&gt;
  32. &lt;LinearLayout
  33. android:layout_width=&quot;0px&quot;
  34. android:layout_height=&quot;0px&quot;
  35. android:focusable=&quot;true&quot;
  36. android:focusableInTouchMode=&quot;true&quot; /&gt;
  37. &lt;androidx.appcompat.widget.SearchView
  38. android:id=&quot;@+id/searchView&quot;
  39. android:layout_width=&quot;match_parent&quot;
  40. android:layout_height=&quot;wrap_content&quot;
  41. android:layout_marginStart=&quot;30dp&quot;
  42. android:layout_marginLeft=&quot;30dp&quot;
  43. android:layout_marginEnd=&quot;35dp&quot;
  44. android:layout_marginRight=&quot;35dp&quot;
  45. android:animateLayoutChanges=&quot;true&quot;
  46. android:layoutDirection=&quot;rtl&quot;
  47. app:iconifiedByDefault=&quot;true&quot;
  48. app:searchHintIcon=&quot;@null&quot; /&gt;
  49. &lt;/com.google.android.material.appbar.MaterialToolbar&gt;
  50. &lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;
  51. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  52. &lt;androidx.recyclerview.widget.RecyclerView
  53. android:id=&quot;@+id/myRecyclerView&quot;
  54. android:layout_width=&quot;match_parent&quot;
  55. android:layout_height=&quot;match_parent&quot;
  56. android:layout_marginTop=&quot;3dp&quot;
  57. app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;/&gt;
  58. &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上没有数据。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:fitsSystemWindows="true"
  7. tools:context=".MainActivity"
  8. xmlns:android="http://schemas.android.com/apk/res/android">
  9. <com.google.android.material.appbar.AppBarLayout
  10. android:id="@+id/tab_anim_appbar"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_gravity="center|top"
  14. android:fitsSystemWindows="true"
  15. android:background="@android:color/transparent"
  16. android:theme="@style/AppTheme.AppBarOverlay"
  17. app:elevation="0dp">
  18. <com.google.android.material.appbar.CollapsingToolbarLayout
  19. android:id="@+id/collapsing_toolbar_layout"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:fitsSystemWindows="true"
  23. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  24. app:toolbarId="@+id/toolbar_scrolling">
  25. <LinearLayout
  26. android:layout_width="0px"
  27. android:layout_height="0px"
  28. android:focusable="true"
  29. android:focusableInTouchMode="true" />
  30. <androidx.appcompat.widget.SearchView
  31. android:id="@+id/searchView"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_marginStart="30dp"
  35. android:layout_marginLeft="30dp"
  36. android:layout_marginEnd="35dp"
  37. android:layout_marginRight="35dp"
  38. android:paddingLeft="16dp"
  39. android:paddingRight="16dp"
  40. android:animateLayoutChanges="true"
  41. android:background="@drawable/rounded_bg"
  42. android:layoutDirection="rtl"
  43. app:iconifiedByDefault="true"
  44. app:searchHintIcon="@null" />
  45. </com.google.android.material.appbar.CollapsingToolbarLayout>
  46. </com.google.android.material.appbar.AppBarLayout>
  47. <androidx.recyclerview.widget.RecyclerView
  48. android:id="@+id/myRecyclerView"
  49. android:layout_width="match_parent"
  50. android:layout_height="match_parent"
  51. android:layout_marginTop="3dp"
  52. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  53. </androidx.coordinatorlayout.widget.CoordinatorLayout>

在活动中:

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

rounded_bg_action_bar.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="@android:color/holo_purple" />
  5. <corners android:bottomLeftRadius="20dp"
  6. android:bottomRightRadius="20dp" />
  7. </shape>

rounded_bg.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="@android:color/holo_purple" />
  5. <corners android:bottomLeftRadius="100dp"
  6. android:bottomRightRadius="100dp" />
  7. </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.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;match_parent&quot;
  5. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  6. android:fitsSystemWindows=&quot;true&quot;
  7. tools:context=&quot;.MainActivity&quot;
  8. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
  9. &lt;com.google.android.material.appbar.AppBarLayout
  10. android:id=&quot;@+id/tab_anim_appbar&quot;
  11. android:layout_width=&quot;match_parent&quot;
  12. android:layout_height=&quot;wrap_content&quot;
  13. android:layout_gravity=&quot;center|top&quot;
  14. android:fitsSystemWindows=&quot;true&quot;
  15. android:background=&quot;@android:color/transparent&quot;
  16. android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;
  17. app:elevation=&quot;0dp&quot;&gt;
  18. &lt;com.google.android.material.appbar.CollapsingToolbarLayout
  19. android:id=&quot;@+id/collapsing_toolbar_layout&quot;
  20. android:layout_width=&quot;match_parent&quot;
  21. android:layout_height=&quot;match_parent&quot;
  22. android:fitsSystemWindows=&quot;true&quot;
  23. app:layout_scrollFlags=&quot;scroll|exitUntilCollapsed&quot;
  24. app:toolbarId=&quot;@+id/toolbar_scrolling&quot;&gt;
  25. &lt;LinearLayout
  26. android:layout_width=&quot;0px&quot;
  27. android:layout_height=&quot;0px&quot;
  28. android:focusable=&quot;true&quot;
  29. android:focusableInTouchMode=&quot;true&quot; /&gt;
  30. &lt;androidx.appcompat.widget.SearchView
  31. android:id=&quot;@+id/searchView&quot;
  32. android:layout_width=&quot;match_parent&quot;
  33. android:layout_height=&quot;wrap_content&quot;
  34. android:layout_marginStart=&quot;30dp&quot;
  35. android:layout_marginLeft=&quot;30dp&quot;
  36. android:layout_marginEnd=&quot;35dp&quot;
  37. android:layout_marginRight=&quot;35dp&quot;
  38. android:paddingLeft=&quot;16dp&quot;
  39. android:paddingRight=&quot;16dp&quot;
  40. android:animateLayoutChanges=&quot;true&quot;
  41. android:background=&quot;@drawable/rounded_bg&quot;
  42. android:layoutDirection=&quot;rtl&quot;
  43. app:iconifiedByDefault=&quot;true&quot;
  44. app:searchHintIcon=&quot;@null&quot; /&gt;
  45. &lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;
  46. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  47. &lt;androidx.recyclerview.widget.RecyclerView
  48. android:id=&quot;@+id/myRecyclerView&quot;
  49. android:layout_width=&quot;match_parent&quot;
  50. android:layout_height=&quot;match_parent&quot;
  51. android:layout_marginTop=&quot;3dp&quot;
  52. app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;/&gt;
  53. &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

In activity:

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

rounded_bg_action_bar.xml

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

rounded_bg.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. android:shape=&quot;rectangle&quot;&gt;
  4. &lt;solid android:color=&quot;@android:color/holo_purple&quot; /&gt;
  5. &lt;corners android:bottomLeftRadius=&quot;100dp&quot;
  6. android:bottomRightRadius=&quot;100dp&quot; /&gt;
  7. &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:

确定