Android 如何在不使用外部库的情况下在recyclerView上实现拉刷新动画?

Android 如何在不使用外部库的情况下在recyclerView上实现拉刷新动画?,android,android-animation,pull-to-refresh,swiperefreshlayout,Android,Android Animation,Pull To Refresh,Swiperefreshlayout,我有一个简单的例子,其中我有一个RecyclerView,当用户在列表顶部时,我希望刷新它!我使用了全新的布局。但是,当刷新列表时,刷新图标将显示在项目的顶部。我想有一个动画,其中列表下降作为图标显示和备份时,刷新完成!基本上我希望它看起来像instagrams刷新动画。我知道这可以通过使用外部库来完成,但我发现的大多数库已经很久没有更新了 这是一个简单的例子,只是为了解释我到目前为止所做的工作: class MainActivity : AppCompatActivity() { p

我有一个简单的例子,其中我有一个RecyclerView,当用户在列表顶部时,我希望刷新它!我使用了全新的布局。但是,当刷新列表时,刷新图标将显示在项目的顶部。我想有一个动画,其中列表下降作为图标显示和备份时,刷新完成!基本上我希望它看起来像instagrams刷新动画。我知道这可以通过使用外部库来完成,但我发现的大多数库已经很久没有更新了

这是一个简单的例子,只是为了解释我到目前为止所做的工作:

class MainActivity : AppCompatActivity() {

    private lateinit var adapter: PersonListAdapter
    private val personList = ArrayList<Person>()
    private lateinit var layoutManager: RecyclerView.LayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        loadData()
        swipe_refresh_layout.setOnRefreshListener {
            swipe_refresh_layout.isRefreshing = false
        }
    }

    private fun loadData() {
        layoutManager = LinearLayoutManager(this)
        adapter = PersonListAdapter(personList, this)

        // RecyclerView setup
        my_recycler_view.layoutManager = layoutManager
        my_recycler_view.adapter = adapter

        // Load data
        for (i in 0..4) {
            personList.add(Person("my name", 20 + i, R.drawable.img1))
            personList.add(Person("me", 10 + i, R.drawable.img1))
        }
        adapter.notifyDataSetChanged()


    }
}
class MainActivity:AppCompatActivity(){
私有lateinit变量适配器:PersonListAdapter
private val personList=ArrayList()
私有lateinit var layoutManager:RecyclerView.layoutManager
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadData()
滑动\u刷新\u布局。设置刷新侦听器{
滑动\u刷新\u布局。isRefreshing=false
}
}
私有数据{
layoutManager=LinearLayoutManager(此)
适配器=personList适配器(personList,this)
//回收视图设置
my_recycler_view.layoutManager=layoutManager
my_recycler_view.adapter=适配器
//加载数据
对于(0..4中的i){
personList.add(Person(“我的名字”,20+i,R.drawable.img1))
personList.add(Person(“我”,10+i,R.drawable.img1))
}
adapter.notifyDataSetChanged()
}
}
这是我的XML:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layoutAnimation="@anim/layout_animation_fall_down"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.v4.widget.SwipeRefreshLayout>

您可以创建自己的
SwipeRefreshLayout
,它应该扩展主SwipeRefreshLayout,然后重写方法


通过查看开源库来获取帮助最简单的方法是使用外部库,为什么需要重新创建控制盘?如果您想学习一些新的东西并提高您的编码技能,我建议您使用最流行的Github repos来刷新实现,并使用您自己的更改深入了解实现细节