I have a simple example where I have a RecyclerView that I want to be refreshed when the user pulls on the top of the list! I have used swipeRefresh layout. However when the list is refreshed the refresh icon is shown on on top of the items. I want to have an animation where the list goes down as the icon is shown and back up when the refresh is done! Basically I want it to look something like instagrams refresh animation. I know that this can be done using external libraries but most of the libraries that I have found have not been updated in a long time!
This is a simple example just to explain what I have done so far:
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()
}
}
This is my 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>