Pull-to-refresh lets users update content by pulling down on the screen. It feels natural and quick, like refreshing a page in a browser.
0
0
Pull-to-refresh in Android Kotlin
Introduction
When showing a list of news articles that can update with new stories.
In a chat app to load new messages by pulling down.
On a weather app to refresh the latest forecast.
In a social media feed to get new posts without restarting the app.
Syntax
Android Kotlin
SwipeRefreshLayout {
setOnRefreshListener {
// Code to refresh data
isRefreshing = false
}
RecyclerView {
// Your list content
}
}Use SwipeRefreshLayout as a container for scrollable views like RecyclerView.
Call isRefreshing = false to stop the loading spinner after refresh.
Examples
Basic setup in Kotlin to listen for pull-to-refresh and stop the spinner.
Android Kotlin
val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipeRefreshLayout)
swipeRefreshLayout.setOnRefreshListener {
// Refresh data here
swipeRefreshLayout.isRefreshing = false
}XML layout wrapping a RecyclerView inside SwipeRefreshLayout for pull-to-refresh.
Android Kotlin
<SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</SwipeRefreshLayout>Sample App
This app shows a list of fruits. Pulling down adds a new fruit at the top and stops the refresh spinner.
Android Kotlin
class MainActivity : AppCompatActivity() { private lateinit var swipeRefreshLayout: SwipeRefreshLayout private lateinit var recyclerView: RecyclerView private val items = mutableListOf("Apple", "Banana", "Cherry") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout) recyclerView = findViewById(R.id.recyclerView) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false) return object : RecyclerView.ViewHolder(view) {} } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { (holder.itemView as TextView).text = items[position] } override fun getItemCount() = items.size } swipeRefreshLayout.setOnRefreshListener { // Simulate refreshing data items.add(0, "New Fruit ${items.size + 1}") recyclerView.adapter?.notifyDataSetChanged() swipeRefreshLayout.isRefreshing = false } } }
OutputSuccess
Important Notes
Make sure your scrollable view supports scrolling to enable pull-to-refresh.
Always stop the refresh animation by setting isRefreshing = false after data loads.
Pull-to-refresh improves user experience by giving a quick way to update content.
Summary
Pull-to-refresh lets users update content by pulling down on a list or scrollable view.
Wrap your scrollable view inside SwipeRefreshLayout and set a refresh listener.
Stop the spinner by setting isRefreshing to false after refreshing.