1.添加依赖
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
2.主界面
package com.example.myapplication3
import android.graphics.color
import android.os.bundle
import android.os.handler
import androidx.appcompat.app.appcompatactivity
import androidx.recyclerview.widget.linearlayoutmanager
import androidx.recyclerview.widget.recyclerview
import androidx.swiperefreshlayout.widget.swiperefreshlayout
class mainactivity : appcompatactivity() {
private lateinit var recyclerview: recyclerview
private lateinit var swipe_view: swiperefreshlayout
private lateinit var viewadapter: recyclerview.adapter<*>
private lateinit var viewmanager: recyclerview.layoutmanager
override fun oncreate(savedinstancestate: bundle?) {
super.oncreate(savedinstancestate)
setcontentview(r.layout.activity_main);
val mydataset = mutablelistof()
for (i in 0 until 20) {
mydataset.add(i.tostring())
}
viewmanager = linearlayoutmanager(this)
viewadapter = myadapter(mydataset)
recyclerview = findviewbyid(r.id.recycle_view).apply {
sethasfixedsize(true)
layoutmanager = viewmanager
adapter = viewadapter
}
swipe_view = findviewbyid(r.id.swipe_view)
swipe_view.setcolorschemecolors(resources.getcolor(r.color.white))
swipe_view.setprogressbackgroundcolorschemeresource(r.color.gray)
swipe_view.setonrefreshlistener {
//your code handler
handler().postdelayed({ swipe_view.isrefreshing = false }, 2000)
}
}
}
?
3.适配器
package com.example.myapplication3
import android.util.log
import android.view.layoutinflater
import android.view.view
import android.view.viewgroup
import android.widget.textview
import androidx.recyclerview.widget.recyclerview
class myadapter(private val mydataset: mutablelist) : recyclerview.adapter() {
class myviewholder(v: view) : recyclerview.viewholder(v) {
val textview: textview
init {
v.setonclicklistener { log.e("tag", "$adapterposition") }
textview = v.findviewbyid(r.id.textviews)
}
}
override fun oncreateviewholder(parent: viewgroup, viewtype: int): myviewholder {
val v = layoutinflater.from(parent.context).inflate(r.layout.layout_item, parent, false);
return myviewholder(v)
}
override fun getitemcount(): int {
return mydataset.size
}
override fun onbindviewholder(holder: myviewholder, position: int) {
holder.textview.text = mydataset[position];
}
companion object {
private val tag = "customadapter"
}
}
4.主界面布局:
5.item布局
6.color资源
#fff
#999
运行效果: