gpt4 book ai didi

android - Android 中 ScrollView 内的 Listview 高度问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:43:46 25 4
gpt4 key购买 nike

我在 Android 的 ScrollView 中使用 ListView。所以,众所周知,会出现高度问题。为此,我使用以下代码设置 ListView 大小:

public static void getListViewSize(ListView myListView, Context context) {
ListAdapter myListAdapter = myListView.getAdapter();

if (myListAdapter == null) {
return;
}

int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {

View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));

WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
@SuppressWarnings("deprecation")
int screenWidth = display.getWidth();
int listViewWidth = screenWidth - 65;
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);

totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
myListView.requestLayout();
}

XML :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
android:id="@+id/lvHomeStream"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp" >
</ListView>

<TextView
android:id="@+id/tvMoreRecords"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/list"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="@string/viewmore"
android:textColor="@color/blue"
android:textSize="16sp" />
</LinearLayout>

</ScrollView>

如果每个 ListView 项目的高度相同,则工作正常。但是,如果每个 ListView 项目的高度不正常,那么它会在 ListView 端边界和 TextView 之间留下额外的大空间。
< br/>请帮我解决这个问题。

最佳答案

好的,我在这里分享我的项目代码可能对你有帮助..

此类计算高度并自动调整布局参数。

解决方案:-

 package com.kview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
import android.widget.ListView;

public class FullLengthListView extends ListView {

boolean expanded = true;

public FullLengthListView(Context context, AttributeSet attrs,
int defaultStyle) {
super(context, attrs, defaultStyle);
}

public FullLengthListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public boolean isExpanded() {
return expanded;
}

public void setExpanded(boolean expanded) {
this.expanded = expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);

android.view.ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}

关于android - Android 中 ScrollView 内的 Listview 高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24362717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com