【问题标题】:Calculating ExpandableListView height at the initial lifecycle在初始生命周期计算 ExpandableListView 高度
【发布时间】:2016-05-04 22:02:16
【问题描述】:

我遇到的问题源于 Android 在嵌套滚动元素方面存在一定的困难。

我有一个 ScrollView,它承载一个水平 RecyclerView 和一个 ExpandableListView 就在它下面。 我遇到的问题是 ScrollView 没有滚动。

我通过以下方式修复了它: 1.设置RecyclerView的固定高度。 2. 计算每个组项单击时的 ExpandableListView 高度。 像这样:

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

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

private void setListViewHeight(ExpandableListView listView, int group) {
        ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
        int totalHeight = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.EXACTLY);
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null, listView);
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

            totalHeight += groupItem.getMeasuredHeight();

            if (((listView.isGroupExpanded(i)) && (i != group)) || ((!listView.isGroupExpanded(i)) && (i == group)))
            {
                for (int j = 0; j < listAdapter.getChildrenCount(i); j++)
                {
                    View listItem = listAdapter.getChildView(i, j, false, null,listView);
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                    totalHeight += listItem.getMeasuredHeight();
                }
            }
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height = totalHeight + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
        if (height < 10)
            height = 200;
        params.height = height;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

问题: 因为我仅在单击组项时计算列表视图高度,所以当我打开片段并直到我单击组项时,ScrollView 不起作用。 我发现在 onCreateView 中计算 ExpandableListView 适配器的高度是有问题的,因为此时它仍然是空的。

任何想法都会非常感激。

【问题讨论】:

    标签: android listview scrollview


    【解决方案1】:

    如果您需要等到视图布局完毕,您可以使用 ViewTreeObserver。例如:

    protected void onCreate(Bundle savedInstanceState) {
    
        ...
    
        final View someView = findViewById(R.id.some_id);
        someView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
    
                // the values are now available
                int mesuredHeight = someView.getMeasuredHeight();
                int height = someView.getHeight();
    
                // done, remove the observer
                someView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    
        ...
    }
    

    【讨论】:

    • 虽然它确实会影响性能.. 似乎每次我触摸屏幕时都会调用它.. 如果我添加一个 if 语句以便它只在第一次调用该方法,问题就会返回.
    • 实际上监听器是在无限循环中调用的。甚至不碰
    • 就是为了避免这种问题,去掉了监听器。如果您触摸视图之前移除监听器会有帮助吗?
    • 那种。性能又好了,但是 ExpandableListView 的高度变得非常小.. 项目之间的滚动显然很烦人,不适用于发布版本
    • 所以这意味着需要多次布局传递,直到可以计算出正确的尺寸。如果没有丑陋的黑客,这种方法可能无法工作。我想知道将ListView 放在Scrollview 中是否首先是一个好主意。无论如何,我建议你不要接受这个答案,对不起。
    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2015-03-22
    • 2021-12-12
    • 1970-01-01
    • 2016-12-25
    • 2021-07-14
    相关资源
    最近更新 更多