繁体   English   中英

展开项目时,RecyclerView向下滚动

[英]RecyclerView scrolls down when expanding item

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

RecyclerView向上滚动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();
    });

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

 TransitionManager.beginDelayedTransition(recyclerView); notifyDataSetChanged(); 

只要您使用更特定的“ notify”方法(例如notifyItemChanged() )而不是notifyDataSetChanged() ,RecyclerView默认就支持动画。 因此,如果可以改用它们,则可以删除TransitionManager调用。

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

这是为您更新的侦听器:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM