【问题标题】:RecyclerView doesn't animate when it's inside a NestedScrollViewRecyclerView 在 NestedScrollView 内时不动画
【发布时间】:2020-08-05 12:51:11
【问题描述】:

我的RecyclerView 在滚动时没有动画。我已将问题缩小到RecyclerView 所在的NestedScrollView。当RecyclerViewNestedScrollView 之外时,它的动画效果很好。但是当它在NestedScrollView 中时,它不会。如何在将RecyclerView 保留在NestedScrollView 中的同时为RecyclerView 设置动画?

以下是相关布局:

<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>

【问题讨论】:

  • 没人?请? :(
  • 我不知道你为什么要把 RecyclerView 放在 NestedScrollVoew 中。这种方式回收将被禁用,因为 NSV 计算大小将强制 RV 膨胀所有 ViewHolders(与列表大小一样多)。
  • 我还能如何在 recyclerView 旁边制作 searchBar(它是 recyclerView 上方的工具栏)?
  • @Rinat 有没有办法在不使用 NestedScrollView 的情况下做到这一点?

标签: java android android-layout android-recyclerview


【解决方案1】:

把 RecyclerView 放到 NestedScrollView 里面不是个好办法。如果你想在RecyclerView上面做SearchBar,可以查看this

您也可以查看this参考。

【讨论】:

  • 我还能如何让搜索栏在 RecycleView 旁边上下滚动?
  • 您可以查看我分享的第二个参考。或者您可以在 ScrollView 中使用带有 RecyclerView 或 EditText 和 RecyclerView 的折叠工具栏。
  • 我今天将尝试折叠工具栏。如果有效,将报告。
  • 我整天都在尝试使用折叠工具栏布局,但它不起作用。如果代码与上面相同,CollapsingToolbarLayout 不应该放在AppBarLayoutMaterialToolbar 之间吗?我应该添加或删除哪些属性?
  • 你能分享一下你的尝试吗?
【解决方案2】:

我检查了您的 xml 并对其进行了修改。它在我身边工作。请根据您的 UI 要求进行修改。

注意:在 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>

【讨论】:

  • 那么我应该删除MaterialToolbar?
  • 是的。我认为你可以设计你想要的东西。
  • 但我需要MaterialToolBar 来创建圆角
  • SearchView 应该位于主工具栏以外的第二个工具栏内
  • 你能告诉我你想要如何布局吗?我无法正确想象。圆边是使用材质工具栏的唯一原因吗?
猜你喜欢
  • 1970-01-01
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多