gpt4 book ai didi

android - 全屏滚动到 ExpandableListView

转载 作者:行者123 更新时间:2023-11-30 02:13:25 31 4
gpt4 key购买 nike

大家好 :) 我使用 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/

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