我已经在带有分页调用addOnScrollListner的recyclerview内部实现了一个recyclerview,但是它没有为子recyclerview触发。
这就是我要实现的:
class NSREActivity : AppCompatActivity() {
private var previousTotal = 0
private var loading = true
private val visibleThreshold = 5
private var firstVisibleItem = 0
private var visibleItemCount = 0
private var totalItemCount = 0
private lateinit var mAdapter: RecyclerGenericKAdapter<HashTags>
private var mList: ArrayList<HashTags> = ArrayList()
private var mChildList: ArrayList<HashTags> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nsre)
initData()
//Usage of generic recycler view
mAdapter = RecyclerGenericKAdapter(mList, R.layout.item_layout, object : RecyclerKallback<HashTags> {
override fun bindData(model: HashTags, position: Int, itemView: View) {
val name = itemView.findViewById<TextView>(R.id.mainName)
val childRv = itemView.findViewById<RecyclerView>(R.id.childRecycler)
name.text = model.tag
itemView.setOnClickListener {
Toast.makeText(this@NSREActivity, model.tag, Toast.LENGTH_SHORT).show()
setChildAdapter(childRv)
}
}
})
recyclerMain.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerMain.adapter = mAdapter
}
private fun setChildAdapter(child : RecyclerView){
val adt = RecyclerGenericKAdapter(mChildList, R.layout.item_sample_rv, object : RecyclerKallback<HashTags> {
override fun bindData(model: HashTags, position: Int, itemView: View) {
val name = itemView.findViewById<TextView>(R.id.text)
name.text = model.tag
itemView.setOnClickListener {
Toast.makeText(this@NSREActivity, model.tag, Toast.LENGTH_SHORT).show()
}
}
})
val mLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
child.layoutManager = mLayoutManager
child.adapter = adt
child.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
visibleItemCount = child.childCount
totalItemCount = mLayoutManager.itemCount
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition()
if (loading) {
if (totalItemCount > previousTotal) {
loading = false
previousTotal = totalItemCount
}
}
if (!loading && totalItemCount - visibleItemCount <= firstVisibleItem + visibleThreshold) {
// End has been reached
Log.e("Yaeye!", "end called")
// Do something
addData()
loading = true
}
}
})
}
}
我已经在带有分页调用addOnScrollListner的recyclerview内部实现了一个recyclerview,但是它没有为子recyclerview触发。 当到达主recyclerview的最后一个索引时,它将为子recyclerview调用addOnScrollListner。
如何在带有分页功能的recyclerview中调用recyclerview?
答案 0 :(得分:0)
我不确定这是否是正确的方法,但是我遇到了类似的问题,并且设法通过以下方式解决了该问题:
我没有检查滚动,但是我正在检查最后一个项目何时绑定:
class PaginationAdapter : RecyclerView.Adapter<RecyclerView.Holder> {
var lastItemCreatedHandler: (() -> Unit)? = null
...
override fun onBindViewHolder(holder: RecyclerView.Holder, position: Int) {
if (position == getItemCount() - 1) {
lastItemCreatedHandler?.invoke()
}
...
}
}
然后,如果应该显示更多项目,我将显示进度,下载更多数据等等。