展开项目时,RecyclerView向下滚动

时间:2018-09-23 14:45:49

标签: java android android-recyclerview

我有一个RecyclerView,它显示列表项。当用户单击它们时,这些项目会展开。显示动画效果很好,但是每次单击某个项目时,RecyclerView似乎都将一个项目添加到了最高位置,但随后将其隐藏起来,使其向下滚动,如下面的GIF中所示。

RecyclerView scrolls up GIF

我的列表项布局:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:paddingBottom="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/txv_item_history_bill_type"
    app:layout_constraintTop_toBottomOf="@+id/txv_item_history_bill_type">

    <ImageView
        android:id="@+id/imv_item_history_category"
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/txv_item_history_bill_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.15"
        android:textAllCaps="true"
        app:layout_constraintStart_toEndOf="@+id/imv_item_history_category"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txv_item_history_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:fontFamily="sans-serif"
        android:letterSpacing="0"
        android:textAllCaps="false"
        android:textColor="@color/colorAccentTextColor"
        android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/imv_item_history_category"
        app:layout_constraintTop_toBottomOf="@+id/txv_item_history_bill_type" />

    <TextView
        android:id="@+id/txv_item_history_date_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:fontFamily="sans-serif-medium"
        android:letterSpacing="0.1"
        android:textAllCaps="false"
        android:textSize="14sp"
        app:layout_constraintStart_toEndOf="@+id/imv_item_history_category"
        app:layout_constraintTop_toBottomOf="@+id/txv_item_history_description" />


</android.support.constraint.ConstraintLayout>

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="72dp"
    android:scrollbars="none">

    <LinearLayout
        android:id="@+id/ll_item_history_bill_action_buttons_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="gone">

        <android.support.design.button.MaterialButton
            android:id="@+id/btn_item_history_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="@string/btn_edit"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton" />

        <android.support.design.button.MaterialButton
            android:id="@+id/btn_item_history_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="@string/btn_delete" />

        <android.support.design.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="@string/btn_edit_amount"
            style="@style/Widget.MaterialComponents.Button.TextButton"/>

        <android.support.design.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="@string/btn_edit_description"
            style="@style/Widget.MaterialComponents.Button.TextButton"/>

        <android.support.design.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_change_category"
            style="@style/Widget.MaterialComponents.Button.TextButton"/>
    </LinearLayout>
</HorizontalScrollView>

<View
    android:id="@+id/view_item_history_divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="8dp"
    android:layout_marginStart="72dp"
    android:background="@color/colorDivider" />

有效代码:

    final boolean isExpanded = position == mExpandedPosition;
    holder.mLlBillActionButtonsContainer.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
    holder.itemView.setActivated(isExpanded);

    holder.itemView.setOnClickListener(v -> {
        mExpandedPosition = isExpanded ? -1 : position;
        TransitionManager.beginDelayedTransition(recyclerView);
        notifyDataSetChanged();
    });

1 个答案:

答案 0 :(得分:0)

我相信这是由于代码中这两行的结合而发生的:

TransitionManager.beginDelayedTransition(recyclerView);
notifyDataSetChanged();

RecyclerView默认情况下支持动画,只要您使用更具体的“通知”方法(例如notifyItemChanged())而不是notifyDataSetChanged()。因此,如果可以改用它们,则可以删除TransitionManager调用。

好消息是,您知道之前“扩展”了哪个职位,因此可以仅在旧职位和新职位上致电notifyItemChanged()。这样可以使您获得更好的动画效果。

这是为您更新的侦听器:

holder.itemView.setOnClickListener(v -> {
    int previousExpandedPosition = mExpandedPosition;
    mExpandedPosition = isExpanded ? -1 : position;

    if (previousExpandedPosition != -1) {
        notifyItemChanged(previousExpandedPosition);
    }
    if (mExpandedPosition != -1) {
        notifyItemChanged(mExpandedPosition);
    }
});