如何动态扩展ListView高度android?

时间:2012-09-27 06:27:23

标签: android android-layout android-widget

要增加列表视图高度,我使用exbandable ListView as,

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 as,

<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" />

但是,列表的最后一项没有正确显示。我也使用了实用程序类,但list中没有变化。还有其他解决方案来扩展视图。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

更改行

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

到此:

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

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

答案 1 :(得分:1)

此问题已经发布。请查看此问题,您可以根据需要在展开式列表中自定义

此链接可以帮助您:

How to change ListView height dynamically in Android?