最近碰到ListView嵌套ListView,最外层的ListView的OnItemClickListener在点击里层ListView的item的时候不响应,在网上查到诸多资料,大部分说在子ListView的根布局中加入
android:descendantFocusability="blocksDescendants"
加了之后点击还是有问题,想到子ListView抢占了最外层ListView的点击事件,所以把里层的ListView的拦截事件给重新写了一遍,将
dispatchTouchEvent(MotionEvent ev)
onTouchEvent(MotionEvent ev)
全部都返回false,本人估计只需要将拦截事件返回false应该就行了,但是没试,有兴趣的可以试试。onInterceptTouchEvent(MotionEvent ev)
回正题,改完这个之后再测试,发现点击事件不触发,于是在外层ListView的xml文件的根节点中加入package com.example.buycar.ui; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ListView; /** * Created by Administrator on 2016/11/25. */ public class MyListView extends ListView { public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //设置为Integer.MAX_VALUE>>2 是listview全部展开 int measureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST); //设置为400是设置listview的高度只能有400 不全部展开 实现可以滑动的效果 int measureSpec1 = MeasureSpec.makeMeasureSpec(400, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, measureSpec); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { return false; } @Override public boolean onTouchEvent(MotionEvent ev) { return false; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return false; } }
重新跑项目,外层ListView的item点击事件正常触发。android:descendantFocusability="blocksDescendants"