DrawerLayout中ListView无法滚动问题求助
解决DrawerLayout中ListView无法滚动的问题
问题根源分析
- 布局重叠拦截触摸事件:
layout_evennements中存在一个无位置约束的自定义ListViewForEmbeddingInScrollView,它会覆盖在目标listview_evennements上方,抢占所有触摸操作,导致真正的列表无法响应滚动。 - ListView高度约束错误:目标ListView仅设置了
layout_below="@id/dashmap2"但未限制底部边界,同时高度设为match_parent,导致其高度超出可用滚动区域。 - 父布局拦截事件:最外层
RelativeLayout设置了android:clickable="true"和android:focusable="true",会拦截ListView的触摸事件。
解决方案步骤
1. 移除无用的重叠ListView
删除layout_evennements中无意义的自定义ListView控件:
<!-- 移除这行无用代码 --> <!-- <com.gabontech.gprstest.utils.ListViewForEmbeddingInScrollView android:layout_width="match_parent" android:layout_height="match_parent"/> -->
2. 修正ListView的布局约束
给底部的空LinearLayout添加id,并让ListView填充顶部栏和底部栏之间的空间:
<!-- 给底部LinearLayout添加id --> <LinearLayout android:id="@+id/bottom_bar" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/listview_evennements" android:layout_gravity="center" android:background="@color/white" android:orientation="horizontal"> </LinearLayout>
修改目标ListView的属性,添加底部约束:
<ListView android:id="@+id/listview_evennements" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/dashmap2" android:layout_above="@id/bottom_bar" <!-- 新增底部边界约束 --> android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:listSelector="#BFB9AF" />
3. 移除父布局的事件拦截属性
删除最外层RelativeLayout的clickable和focusable属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:id="@+id/layout_evennements" android:theme="@style/Theme.GPRStest.NoActionBar" android:layout_height="match_parent" <!-- 移除以下两个属性 --> <!-- android:clickable="true" --> <!-- android:focusable="true" --> >
4. 调整NavigationView的子布局高度
将activity_main中include的布局高度从match_parent改为wrap_content,避免超出NavigationView的可用空间:
<include layout="@layout/layout_evennements" android:layout_width="match_parent" android:layout_height="wrap_content"/> <!-- 修改为wrap_content -->
额外排查点
如果仍存在滚动问题,可以检查:
- ListView的适配器是否正确绑定数据,确保item布局高度符合预期
- 给ListView添加
android:descendantFocusability="blocksDescendants"属性,避免item内部控件抢占焦点
内容的提问来源于stack exchange,提问作者Leyoussi Bonachy Lauriel
相关产品推荐
相关产品推荐

