【问题标题】:Swipe to another view in recyclerView with ItemTouchHelper使用 ItemTouchHelper 在 recyclerView 中滑动到另一个视图
【发布时间】:2015-07-02 14:55:40
【问题描述】:

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

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

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

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

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

谢谢。

【问题讨论】:

    标签: android animation view android-recyclerview


    【解决方案1】:

    我正在处理类似的任务 - 需要在项目滑开时显示隐藏的底层面板。 我已经设法在项目行视图的根 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);
            }
        }
    }
    

    它有助于滑开一个视图并显示底层视图。

    【讨论】:

    • 像魅力一样工作。
    猜你喜欢
    • 2017-02-07
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多