android - 如何通过代码触发滚动动画?

标签 android listview animation android-canvas overscroll

我正在尝试通过代码触发 ListView 上的滚动动画。 enter image description here

波纹或辉光(你怎么调用它?)应该出现在边缘。

我尝试了 ListView 中的所有功能,如 scroll* 或 scrollSmooth*,都不能触发动画。

我也尝试了一些可以触发波纹背景动画的解决方案,但它与这种边缘效果不一样。

我是 Android 新手。有什么方法请告知,谢谢。

我也尝试过使用onDraw(Canvas canvas),因为我没有找到任何解决方案。因为我总是在 iOS 上做这种事情,所以我可以将效果绘制为动画。但是这里还有一个问题,画图速度超低,不知道为什么,就是stackoverflow上一些典型的画图代码。当我稍后开始 View 转换时,一切都搞砸了。

最佳答案

我建议您使用 RecyclerView 代替您的目的。默认支持“Overscroll”动画。

RecyclerView 在某种程度上与 ListView 非常相似,但它使处理动画或强制您使用 等方式变得更容易ViewHolder 模式。此外,它比 ListView 更灵活。

然而,在我个人看来,对于简单的用例,使用 ListView 而不是 RecyclerView 仍然很好。

那么创建一个需要哪些步骤呢?

添加依赖

首先将依赖项添加到您的 app.gradle 文件中。您将在 "Gradle Scripts -> build.gradle (Module: app)" 下找到此文件,或者只需按 TAB 两次 然后在搜索菜单中输入 gradle(漂亮搜索文件时很有用:))。添加以下依赖项:implementation 'com.android.support:recyclerview-v7:26.1.0'

或者,您可以将 RecyclerView 从 Designer 拖放到您的布局(可以通过转到 layout.xml 文件并切换到 来找到 Designer >Design Tab) 通过搜索它(左上角)。这将自动为您添加依赖项。

创建布局

简单的和ListView一样(注意:这个例子没有使用ConstraintLayout作为parent ViewGroup):

    <android.support.v7.widget.RecyclerView
    android:id="@+id/rvExample"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

我不知道您目前是否正在使用 ConstraintLayout,但您绝对应该检查一下:)。

适配器说明

public class RecyclerViewAdapter extends RecyclerView.Adapter<ExampleViewHolder> {
private List<String> mData;
private LayoutInflater mInflater;

RecyclerViewAdapter(List<String> data, LayoutInflater inflater) {
    mData = data;
    mInflater = inflater;
}

@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ExampleViewHolder(mInflater.inflate(R.layout.my_layout, parent, false));
}

@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
    holder.getTvText().setText(mData.get(position));
}

@Override
public int getItemCount() {
    return mData.size();
}
}

onCreateViewHolder:如前所述,必须创建一个新的 ViewHolder每次滚动时屏幕上弹出新项目时都不会调用此函数!

onBindViewHolder:必须更新作为参数给出的 ViewHolder 的数据。

getItemCount:这个 RecyclerView 包含多少个项目?

ViewHolder

class ExampleViewHolder extends RecyclerView.ViewHolder {
private TextView mTvText;

    public ExampleViewHolder(View itemView) {
        super(itemView);
        mTvText = itemView.findViewById(R.id.tv_text);
    }

    public TextView getTvText() {
        return mTvText;
    }
}

您只需提取您的 View ,以便在调用 onBindViewHolder 方法时更新它们。它的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="56dp">
<TextView
    android:id="@+id/tv_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</android.support.constraint.FrameLayout>

完成 现在您只需为您的 RecyclerView 设置一个 LayoutManager 和一个 Adapter ,瞧! LayoutManager 只是告诉 RecyclerView 这些项目应该如何排列。好的,现在让我们完成:

mRvExample.setHasFixedSize(true); //set this if every item has the same size - performance gain
mRvExample.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRvExample.setAdapter(new RecyclerViewAdapter(Arrays.asList("asdf","asdfsdf"), getLayoutInflater());

虽然我无法使用 ListView 直接解决您的问题,但我希望我仍然能够帮助您。由于我不知道您处于哪个级别,我希望我也能提供有关如何设置基本 RecyclerView 的说明。您应该查看它以获得更深入的了解的有用链接是:

基地描述: https://developer.android.com/training/material/lists-cards.html

谈论进入动画: https://proandroiddev.com/enter-animation-using-recyclerview-and-layoutanimation-part-1-list-75a874a5d213

添加 onClick:RecyclerView onClick

使用 Kotlin :https://antonioleiva.com/recyclerview-adapter-kotlin/

元素装饰:https://www.bignerdranch.com/blog/a-view-divided-adding-dividers-to-your-recyclerview-with-itemdecoration/

Pro RecyclerView 讨论:https://www.youtube.com/watch?v=KhLVD6iiZQs

关于android - 如何通过代码触发滚动动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48133119/

相关文章:

android - 删除容器时不会删除动态添加的 fragment

android - 将父 fragment 转换为主 fragment 时出现空指针异常

java - listView 中的加载栏

objective-c - 如何在Cocoa中从NSButton的中心点顺时针方向旋转NSButton?

javascript - 需要 SVG 解决方法

html - Firefox 动画播放状态未正确更新伪元素

android - 几个 android-support-v4,我该如何删除它们?

android - ListView 内的 map fragment ?

android - 从微调器中选择项目时未显示 ListView

android - 类型安全 : Unchecked cast from Object to List<MyObject>