android - 全屏滚动到 ExpandableListView

标签 android expandablelistview android-xml

大家好 :) 我使用 ExpandableListView。我的 ExpandableList 位于屏幕的一部分。我期待的结果是展开列表时,而不是增加整个屏幕的大小。但是我的列表也有自己的卷轴。 (wrap_content、match_parent、Ndp都是自己的scroll。)请看图。

enter image description here



你能在 ExpandableList 中看到滚动条吗? 我想要可扩展列表滚动整个屏幕。



我的 xml 代码(有问题的不必要部分已删除):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:orientation="horizontal" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:orientation="horizontal" />

                <ExpandableListView
                    android:id="@+id/pastMedalList"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:layout_marginTop="15dp"
                    android:groupIndicator="@null"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

最佳答案

我自己解决了。我在 Activity 中添加了这个方法。

private void setExpandableListViewHeight(ExpandableListView listView, int group) {
    ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
    if (listAdapter == null) {
        return;
    }

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        view = listAdapter.getGroupView(i, false, view, listView);
        if (i == 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
        if(((listView.isGroupExpanded(i)) && (i != group)) || ((!listView.isGroupExpanded(i)) && (i == group))) {
            View listItem = null;
            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                listItem = listAdapter.getChildView(i, j, false, listItem, listView);
                listItem.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, View.MeasureSpec.UNSPECIFIED));
                listItem.measure(
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                totalHeight += listItem.getMeasuredHeight();
            }
        }
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

并按如下方式使用:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.expandable, container, false);

    ExpandableAdapter adapter = new ExpandableAdapter();
    expandableListView.setAdapter(adapter);
    setExpandableListViewHeight(expandableListView, -1);
    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
            setExpandableListViewHeight(parent, position);
            return false;
        }
    });
    return view;
}

关于android - 全屏滚动到 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785780/

相关文章:

java - getmeasuredheight 在使用 ReplacementSpan 时返回 0 或相同

android - 如何在 ScrollView 中完全实现 ListView

Android如何创建堆栈类图像背景

android - 当我的应用程序在后台运行时,如何让android系统杀死它?

android - Messenger InstantGames - Web 请求在 Messenger 网页上运行良好,但在通过 Messenger 移动应用程序播放时失败

java - Android 中是否有类似于 C/C++ 中包含程序主循环的 "int main"的函数?

android - Expandable ListView 中的 hasStableIds ()?

expandableList 中的 android editText 错误索引和错误计数?

java - ExpandableListView 不调用 getGroupView 也不填充

使用 SAX 解析器的 Android XML 解析问题