第一种:
class MyListView : ListView {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//解决ScrollView嵌套Listview只显示第一行的问题
val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
500, // 固定高度(实际中这个值应该是根据手机屏幕计算出来的)
MeasureSpec.AT_MOST
)
super.onMeasure(widthMeasureSpec, newHeightMeasureSpec)
}
//解决滑动冲突
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
when (ev.action) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> parent.requestDisallowInterceptTouchEvent(
true
)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> parent.requestDisallowInterceptTouchEvent(
false
)
}
return super.onInterceptTouchEvent(ev)
}
}
第二种:
listView.setOnTouchListener(View.OnTouchListener { v, event -> // 当触摸的是Listview & 当前Listview可滚动时,则将事件交给EditText处理;
when (event.action) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> v.parent.requestDisallowInterceptTouchEvent(
true
)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> v.parent.requestDisallowInterceptTouchEvent(
false
)
}
false
})