gpt4 book ai didi

android - 使用 ItemTouchHelper 滑动到 recyclerView 中的另一个 View

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:58 24 4
gpt4 key购买 nike

给定一个 RecyclerView。在执行滑动时显示不同的 View 的最佳做法和最简单的方法是什么?

Here是一个类似的问题。但是使用该解决方案只能显示位图。

recyclerview 带来了很棒的 ItemTouchHelper ,它有回调:

 public void onChildDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {...}

我的问题是:我能否以某种方式利用此回调在两个 View 之间滑动,如果可以,如何。

谢谢。

最佳答案

我正在处理类似的任务 - 需要在滑动项目时显示隐藏的底层面板。我已经设法做到了,在项目行 View 的根 RelativeLayout 中有两个内部布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
android:id="@+id/actionsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hidden stuff is here"/>
</RelativeLayout>
<RelativeLayout android:id="@+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@color/submission_row_bg">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Swiping stuff is here"/>

</RelativeLayout>

</RelativeLayout>

在您的 ItemViewHolder 类中,我有相应的字段:

public class ItemViewHolder extends RecyclerView.ViewHolder {

RelativeLayout itemLayout;
RelativeLayout actionsLayout;
}

然后在 ItemTouchHelper.Callback 中,我重写了这样的 onChildDraw 方法:

public static class MyItemTouchCallback extends ItemTouchHelper.Callback {

public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {

if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
//HERE IS THE TRICK
((ItemViewHolder) viewHolder).itemLayout.setTranslationX(dX);

} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY,
actionState, isCurrentlyActive);
}
}
}

它有助于滑动一个 View 并显示下面的 View 。

关于android - 使用 ItemTouchHelper 滑动到 recyclerView 中的另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187978/

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