- RecyclerView嵌套(含滑动冲突解决,在子控件中解决冲突
- android的RecyclerView嵌套在NestedScr
- android解决RecyclerView嵌套在NestedSc
- RecyclerView的item中嵌套Scrollview的滑
- NestedScrollView+RecyclerView
- Android ScrollView嵌套Recyclerview
- CoordinatorLayout CollapsingToo
- 解决BottomSheetDialog与RecyclerView
- 解决ScrollView嵌套RecyclerView滑动冲突问题
- ScrollView嵌套RecyclerView滑动冲突解决
背景
有时需要在纵向滑动列表中嵌套横向滑动列表.
效果图:
![](https://img.haomeiwen.com/i6169789/e6790eadf5837cc5.gif)
解决方案
通过纵向RecyclerView嵌套横向RecyclerView.
大致实现方式:
- 主界面中包含一个纵向RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
- 纵向RecyclerView的adapter
- VerticalRvAdapter.kt
class VerticalRvAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var mDataList = mutableListOf<List<String>>()
private lateinit var mContext: Context
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val data = mDataList[position]
val horizontalRvAdapter = HorizontalRvAdapter()
holder.itemView.horizontalRecyclerView.adapter = horizontalRvAdapter
horizontalRvAdapter.setData(data)
}
fun setData(dataList: List<List<String>>) {
mDataList.clear()
mDataList.addAll(dataList)
notifyDataSetChanged()
}
override fun getItemCount(): Int = mDataList.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
mContext = parent.context
val view = LayoutInflater.from(mContext).inflate(R.layout.item_vertical_rv, parent, false)
view.horizontalRecyclerView.layoutManager = LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false)
return ViewHolder(view)
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!)
}
- 纵向RecyclerView每个item对应的布局item_vertical_rv.xml(包含一个横向RecyclerView ):
<com.cxyzy.demo.HorizontalRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
- 横向RecyclerView(HorizontalRecyclerView.kt):
作用: 解决滑动冲突,具体多大角度进行纵向滑动,多大角度进行横向滑动,可以通过修改if (abs(moveX - mDownX) > abs(moveY - mDownY))
实现.(此处为了代码简洁,为直接计算出角度,而是采用宽和高的比较来达到同样效果)
class HorizontalRecyclerView(context: Context, attrs: AttributeSet) : RecyclerView(context, attrs) {
private var mDownX: Int = 0
private var mDownY: Int = 0
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
when (ev.action) {
MotionEvent.ACTION_DOWN -> {
mDownX = ev.x.toInt()
mDownY = ev.y.toInt()
}
MotionEvent.ACTION_MOVE -> {
val moveX = ev.x.toInt()
val moveY = ev.y.toInt()
if (abs(moveX - mDownX) > abs(moveY - mDownY)) {
parent.requestDisallowInterceptTouchEvent(true)
}
mDownX = moveX
mDownY = moveY
}
}
return super.dispatchTouchEvent(ev)
}
}
源代码
https://gitee.com/hspbc/recyclerViewDemo/tree/master/nestedDemo
网友评论