List paging and drop-down refresh are common features of list pages. In Android development, lists are generally implemented by ListView or RecyclerView.
RecyclerView is more convenient than ListView, but both RecyclerView and ListView have no list paging and drop-down refresh function. This time has RecyclerView as an example, use as little code as possible, let RecyclerView with pull down and bottom pull up listening.
I’m using the Kotlin extension function.
There are no renderings this time.
Direct masturbation code:
-
- First of all, to use the drop-down effect of Android SwipeRefreshLayout, so you need to use a SwipeRefreshLayout outside of the RecyclerView layout file. (You can use this layout file as a template to include pages that require lists.)
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
Copy the code
- 2. Write an interface Listener
interface RecyclerListener {
fun loadMore(a)
fun refresh(a)
}
Copy the code
- 3. Next comes the core code, add an extension to RecycleerView. The following code can be placed in any Kotlin file in the project.
// In the layout, RecyclerView nesting a swipeRefreshLayout, can realize the pull down refresh up load more
fun RecyclerView.setListener(l: RecyclerListener){
setOnScrollListener(object : RecyclerView.OnScrollListener() {
var lastVisibleItem: Int = 0
val swipeRefreshLayout = this@setListener.parent
override fun onScrolled(recyclerView: RecyclerView? , dx:Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy) lastVisibleItem = (recyclerView? .layoutManageras LinearLayoutManager).findLastVisibleItemPosition()
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1=== recyclerView.adapter? .itemCount) {// No more loads can be loaded in the drop-down refresh
if(swipeRefreshLayout is SwipeRefreshLayout){
if(! swipeRefreshLayout.isRefreshing){ l.loadMore() } }else{
l.loadMore()
}
}
}
})
val swipeRefreshLayout = this.parent
if(swipeRefreshLayout is SwipeRefreshLayout){
swipeRefreshLayout.setOnRefreshListener {
l.refresh()
}
}
}
Copy the code
- This is the end of all the preparatory work. Finally, in the entire project, you can add listeners directly where you need to pull down the refresh
recyclerView.setListener(object :RecyclerListener{
override fun loadMore(a){}override fun refresh(a){}})Copy the code
In summary, the trigger of the drop-down refresh is monitored by swipeRefreshLayout, and the trigger of the drop-down is monitored by RecyclerView scrolling to the bottom. Kotlin’s extension function is a handy way to extend functionality without breaking the original code structure.