如何调整事件视图的大小?

时间:2014-02-11 03:24:11

标签: android expandablelistview

我正在使用ScrollView中的可扩展ListView创建自定义可扩展视图。单击可展开的ListView时,它应该展开以覆盖屏幕的2/3。

我试过了:

expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                 Utility.setListViewHeightBasedOnChildren(expListView);
                return false;
            }
        });

Utility.java:

public class Utility {
    public static int original_height;
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter
                .getCount() - 1))) / 2;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

如果放置

,相同的代码将起作用
Utility.setListViewHeightBasedOnChildren(expListView);
设置列表适配器后

此代码。

0 个答案:

没有答案