【问题标题】:Animate recycler view items left-right to tell user of swipe options左右动画回收器视图项目以告诉用户滑动选项
【发布时间】:2020-04-18 21:58:04
【问题描述】:

我已经实现了一个手势来提示用户我们在 iOS 应用中拥有的滑动选项。它看起来像-

我是 Android 应用开发的新手。我已经实现了除了动画之外的所有东西,比如屏幕。如何在 Android 应用程序中为回收站视图项添加相同的动画?

【问题讨论】:

    标签: android android-animation android-recyclerview


    【解决方案1】:

    最后,我自己实现了。

    这里是item_layout.xml-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <View
            android:id="@+id/leftView"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/main_layout"
            android:layout_alignBottom="@id/main_layout"
            android:layout_alignParentStart="true"
            android:background="@color/darkGray" />
    
        <View
            android:id="@+id/rightView"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/main_layout"
            android:layout_alignBottom="@+id/main_layout"
            android:layout_alignParentEnd="true"
            android:background="@color/darkRed" />
    
        <RelativeLayout id="@+id/main_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </RelativeLayout>    
    
    </RelativeLayout>
    

    这是RecyclerView.Adapter 的实现-

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
    
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ....
        ....
    
        if (position == 0) {
            ObjectAnimator animationLeft = ObjectAnimator.ofFloat(holder.swipeLayout, "translationX", 0f, 80f, 0f, -80f, 0f);
            animationLeft.setDuration(1500);
            animationLeft.start();
        }
    }
    

    希望这对某人有所帮助!

    【讨论】:

    • 唯一的问题是动画可能在视图绘制之前被调用。
    • 我不确定何时会发生这种情况。你注意到了吗?就我而言,它仍然运行良好。为什么让你这么说?在这种情况下,我可能会更新方法。
    • 从服务器加载数据时,获取数据并填充视图需要几毫秒。例如,当我将其设置为 if (position == 4) { 时,动画就会起作用。
    • 哦,我现在可以看到了。但我不认为在 onBindViewHolder 中获取数据是一个好主意。你指的是这个吗?
    • 就我而言,我在加载时从服务器获取日期,从响应对象创建列表,该列表用于填充适配器。现在,对于列表中的每个项目,将调用 onBindViewHolder 并将返回填充的 ViewHolder。绑定完成后,动画将立即开始。
    猜你喜欢
    • 2018-05-09
    • 2013-07-05
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多