我知道如何获取一级的,那我想知道如何获取二级列表的所有数据呢 ??
花点时间看一下BaseQuickAdapter内的代码实现
private var mOnItemClickListener: OnItemClickListener? = null
private var mOnItemLongClickListener: OnItemLongClickListener? = null
private var mOnItemChildClickListener: OnItemChildClickListener? = null
private var mOnItemChildLongClickListener: OnItemChildLongClickListener? = null
/**
* 绑定 item 点击事件
* @param viewHolder VH
*/
protected open fun bindViewClickListener(viewHolder: VH, viewType: Int) {
mOnItemClickListener?.let {
viewHolder.itemView.setOnClickListener { v ->
var position = viewHolder.adapterPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnClickListener
}
position -= headerLayoutCount
setOnItemClick(v, position)
}
}
mOnItemLongClickListener?.let {
viewHolder.itemView.setOnLongClickListener { v ->
var position = viewHolder.adapterPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnLongClickListener false
}
position -= headerLayoutCount
setOnItemLongClick(v, position)
}
}
mOnItemChildClickListener?.let {
for (id in getChildClickViewIds()) {
viewHolder.itemView.findViewById<View>(id)?.let { childView ->
if (!childView.isClickable) {
childView.isClickable = true
}
childView.setOnClickListener { v ->
var position = viewHolder.adapterPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnClickListener
}
position -= headerLayoutCount
setOnItemChildClick(v, position)
}
}
}
}
mOnItemChildLongClickListener?.let {
for (id in getChildLongClickViewIds()) {
viewHolder.itemView.findViewById<View>(id)?.let { childView ->
if (!childView.isLongClickable) {
childView.isLongClickable = true
}
childView.setOnLongClickListener { v ->
var position = viewHolder.adapterPosition
if (position == RecyclerView.NO_POSITION) {
return@setOnLongClickListener false
}
position -= headerLayoutCount
setOnItemChildLongClick(v, position)
}
}
}
}
}
/**
* override this method if you want to override click event logic
*
* 如果你想重新实现 item 点击事件逻辑,请重写此方法
* @param v
* @param position
*/
protected open fun setOnItemClick(v: View, position: Int) {
mOnItemClickListener?.onItemClick(this, v, position)
}
/**
* override this method if you want to override longClick event logic
*
* 如果你想重新实现 item 长按事件逻辑,请重写此方法
* @param v
* @param position
* @return
*/
protected open fun setOnItemLongClick(v: View, position: Int): Boolean {
return mOnItemLongClickListener?.onItemLongClick(this, v, position) ?: false
}
protected open fun setOnItemChildClick(v: View, position: Int) {
mOnItemChildClickListener?.onItemChildClick(this, v, position)
}
protected open fun setOnItemChildLongClick(v: View, position: Int): Boolean {
return mOnItemChildLongClickListener?.onItemChildLongClick(this, v, position) ?: false
}
使用 setOnItemChildClick(v: View, position: Int)方法可以监听到“嵌套RecycleView item”
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Tips.show("嵌套RecycleView item 收到: " + "点击了第 " + position + " 一次");
}