gpt4 book ai didi

java - 展开项目时 RecyclerView 向下滚动

转载 作者:行者123 更新时间:2023-12-02 10:47:07 24 4
gpt4 key购买 nike

我有一个显示列表项的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();
});

最佳答案

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

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);
}
});

关于java - 展开项目时 RecyclerView 向下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52467241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com