Android 我的ListView不滚动
我试图创建一个带有静态标题和可滚动动态列表的活动。布局如下:Android 我的ListView不滚动,android,listview,scroll,Android,Listview,Scroll,我试图创建一个带有静态标题和可滚动动态列表的活动。布局如下: <GridLayout android:id="@+id/GridLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="1" > <LinearLayout android:layout_width="match_parent" android:la
<GridLayout
android:id="@+id/GridLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="@+id/listex"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
我尝试使用height=0dp&weight=1解决方案,但结果是,当我向列表中添加一些内容时,它不会显示任何元素
如果使用height=match\u parent&weight=1,则会显示元素,但不可滚动
请帮忙,我尝试了很多组合都没有结果
谢谢。您在GridLayout中的列表删除列表视图的父视图GridLayout,它将完美滚动
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="@+id/listex"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
垂直滚动
GridLayout
不应包含另一个垂直滚动小部件(ListView)。您不应该将列表视图
放在滚动视图
中,因为列表视图
类实现了它自己的滚动,它只是不接收手势,因为它们都是由父级处理的,父级是它的滚动视图设置为线性布局权重总和=1
,您可以尝试使用下面的命令代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="static header" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
我一直在使用此列表视图,效果很好。
也许您的列表视图中没有足够的项目,无法滚动
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="290dp"
android:layout_weight="0.50" >
</ListView>
这是我的解决方案
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:fadeScrollbars="true"
android:layout_weight="1" />
将ListView权重设置为1。或者你可以这样做。
在设置适配器之后,调用此方法并在dat之后设置adapternotifydatasetchange()
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
public static void setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null)
回来
int desiredWidth=View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
视图=空;
对于(int i=0;i
对于您的listview,高度应与父项匹配,listview中是否有足够的项目可以滚动?可以发布屏幕截图吗?垂直滚动的GridView不应该包含另一个垂直滚动的小部件(ListView)