gpt4 book ai didi

android - 如何动态扩展 ListView 高度 android?

转载 作者:行者123 更新时间:2023-11-29 18:07:30 25 4
gpt4 key购买 nike

为了增加 ListView 的高度,我使用可扩展的 ListView 作为,

public class ExpandableListView extends ListView {

private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;

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

@Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
System.out.println("params h "+params.height+" "+params);
setLayoutParams(params);
}

super.onDraw(canvas);
}

}

layout.xml 作为,

<com.jems.realtimedata.ExpandableListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:background="@color/list_back"
android:cacheColorHint="#00000000"
android:divider="@drawable/line"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="10dp"
android:scrollbars="none" />

但是,列表的最后一项显示不正确。我也使用了实用程序类,但列表没有变化。任何其他扩展 View 的解决方案。请帮助我解决这个问题。

最佳答案

换行

params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);

为此:

params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() + 1 : 0);

这仅适用于包含单行项目的列表。

关于android - 如何动态扩展 ListView 高度 android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12615349/

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