1、创建entity类
class InternentBarEntity {
var bname: String? = null
var barea: String? = null
var badddata: String? = null
var bimage: String? = null
var bid: String? = null
}
2、创建adapter类
/**
*作者:created by meixi
*邮箱:15913707499@163.com
*日期:2019/5/29 10
*/
class InternetBarAdapter( private val entityList: List) : RecyclerView.Adapter() {
private var mOnItemClickListener: OnItemClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.tab_item, null)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val entity = entityList[position]
try {
holder.item_intnbname.text = entity.bname
} catch (e: Exception) {
e.printStackTrace()
}
//判断是否设置了监听器
if (mOnItemClickListener != null) {
//为ItemView设置监听器-
holder.itemView.setOnClickListener {
val position = holder.layoutPosition // 1
mOnItemClickListener!!.onItemClick(holder.itemView, position) // 2
}
}
}
override fun getItemCount(): Int {
return entityList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var item_intnbname = itemView.findViewById(R.id.tab_tv) as TextView
}
// 删除 打勾 全选
fun cleckAll(is_checked: Int) { //全选 删除多少那里要删除全部
for (a in entityList.indices) {
if (is_checked == a) {
entityList.get(a).bname = "选中了"
} else {
}
}
notifyDataSetChanged()
}
interface OnItemClickListener {
fun onItemClick(view: View, position: Int)
}
fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
this.mOnItemClickListener = onItemClickListener
}
}
3、添加recyclerview依赖
compile('com.android.support:recyclerview-v7:25.1.1') {
force = true
}
4、加载数据实现kotlin 代码
private val barEntityList = ArrayList()
private var barAdapter: InternetBarAdapter? = null
private var lastposion: Int = 0
private var pagenum = 1
private var pageContent:Int = 0
private var pageSize = 10
public fun initrecycleView(){
var internentBarEntity2:InternentBarEntity= InternentBarEntity()
internentBarEntity2.badddata = "地址122"
internentBarEntity2.bname="名称111222"
barEntityList.add(internentBarEntity2)
for (a in 0..19) {
var internentBarEntity:InternentBarEntity=InternentBarEntity()
internentBarEntity.badddata = "地址1"
internentBarEntity.bname="名称111=="+a
barEntityList.add(internentBarEntity)
// if (a==6){
// break
// }
}
barAdapter = InternetBarAdapter( barEntityList)
barAdapter!!.setOnItemClickListener(object : InternetBarAdapter.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
barAdapter!!.cleckAll(position)
LgqLogutil.e("ITEM"+position)
}
})
mRecyclerView.layoutManager = LinearLayoutManager(context)
mRecyclerView.adapter = barAdapter
mRecyclerView.isNestedScrollingEnabled = false//禁止滑动
mRecyclerView.isFocusable = false//导航栏切换不再focuse
mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//用来标记是否正在向最后一个滑动,既是否向右滑动或向下滑动
internal var isSlidingToLast = false
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
val manager = recyclerView!!.layoutManager as LinearLayoutManager
// 当不滚动时
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//获取最后一个完全显示的ItemPosition
lastposion = manager.findLastCompletelyVisibleItemPosition()
// Log.i("lgq","..........dddddd====="+"....."+lastposion);
val totalItemCount = manager.itemCount
if (lastposion == totalItemCount - 1 && isSlidingToLast) {
pagenum++
if (pagenum > pageContent) {
Toast.makeText(activity,"yidaodi",Toast.LENGTH_LONG).show()
return
}
}
}
}
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
//dx用来判断横向滑动方向,dy用来判断纵向滑动方向
//大于0表示,正在向右滚动;小于等于0 表示停止或向左滚动
isSlidingToLast = dy > 0
}
})
}
bug在线交流:QQ1085220040
效果: