如何使 ExpandableListView 在 ScrollView 内滚动而不是在其自身上滚动?
How to make ExpandableListView scroll inside ScrollView and not on itself?
我无法让 ExpandableListView 在 ScrollView 内部滚动,而不是在其自身上滚动。问题是,如果我将 ExpandableListView 更改为带有大文本的 TextView,则一切正常。但是 ExpandableListView 滚动可以在没有 ScrollView 的情况下工作。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer"
android:layout_marginBottom="0dp"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ImageGoods"
android:layout_width="match_parent"
android:layout_height="250dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/pizza_pepperoni_lovers" />
<TextView
android:id="@+id/ImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="20dp"
android:layout_alignLeft="@id/ImageGoods"
android:layout_alignTop="@id/ImageGoods"
android:text="NEW Product!!!"
android:textStyle="bold"
android:textColor="@color/Black"
android:textSize="30dp"
android:includeFontPadding="false"
android:background="@color/TextGoodsImageColor"
android:gravity="center"/>
<Button
android:id="@+id/SizeButton1"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginStart="0dp"
android:layout_marginTop="5dp"
android:background="@drawable/button_border"
android:text="25mm"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="@+id/SizeButton2"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="@+id/textView2"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@+id/SizeButton1"
android:background="@drawable/button_border"
android:text="30mm"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="@+id/SizeButton3"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="@+id/textView2"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@+id/SizeButton2"
android:background="@drawable/button_border"
android:text="36mm"
android:textAllCaps="false"
android:textSize="12sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ImageGoods"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="size"
android:textColor="@color/Black"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/SizeButton3"
android:layout_marginTop="7dp">
<ExpandableListView
android:id="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:transcriptMode="disabled"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/colorFooter"
android:orientation="vertical" >
<Button
android:id="@+id/btn_add_goods_to_basket"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="5dp"
android:text="Add basket"
android:inputType="textFilter"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:textColor="@color/White" />
<Button
android:id="@+id/btn_change_qtty_minus"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_marginLeft="7dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:background="@drawable/plus_minus_button"
android:drawableTop="@drawable/minus"
android:gravity="center_horizontal"
android:padding="5dp"
android:textAlignment="center"
android:textColor="@color/White"
android:textSize="25dp" />
<Button
android:id="@+id/btn_change_qtty_pilus"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_marginStart="7dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:layout_toEndOf="@+id/textView5"
android:background="@drawable/plus_minus_button"
android:gravity="center_horizontal"
android:padding="5dp"
android:drawableTop="@drawable/pilus"
android:textAlignment="center"
android:textColor="@color/White"
android:textSize="25dp" />
<TextView
android:id="@+id/TotalSum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_change_qtty_minus"
android:layout_alignBottom="@+id/btn_change_qtty_minus"
android:layout_marginStart="7dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="@+id/btn_change_qtty_pilus"
android:textColor="@color/White"
android:gravity="center"
android:text="25$"
android:textStyle="bold" />
<TextView
android:id="@+id/textView5"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_change_qtty_minus"
android:layout_alignBottom="@+id/btn_change_qtty_minus"
android:layout_marginStart="7dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="@+id/btn_change_qtty_minus"
android:textStyle="bold"
android:textColor="@color/White"
android:gravity="center"
android:text="x1" />
</RelativeLayout>
</RelativeLayout>
这是我的 xml 代码。
如何解决这个问题?
刚刚找到我问题的答案。
@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;
}
private void setExpandableListViewHeight(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();
}
我无法让 ExpandableListView 在 ScrollView 内部滚动,而不是在其自身上滚动。问题是,如果我将 ExpandableListView 更改为带有大文本的 TextView,则一切正常。但是 ExpandableListView 滚动可以在没有 ScrollView 的情况下工作。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer"
android:layout_marginBottom="0dp"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ImageGoods"
android:layout_width="match_parent"
android:layout_height="250dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/pizza_pepperoni_lovers" />
<TextView
android:id="@+id/ImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="20dp"
android:layout_alignLeft="@id/ImageGoods"
android:layout_alignTop="@id/ImageGoods"
android:text="NEW Product!!!"
android:textStyle="bold"
android:textColor="@color/Black"
android:textSize="30dp"
android:includeFontPadding="false"
android:background="@color/TextGoodsImageColor"
android:gravity="center"/>
<Button
android:id="@+id/SizeButton1"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginStart="0dp"
android:layout_marginTop="5dp"
android:background="@drawable/button_border"
android:text="25mm"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="@+id/SizeButton2"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="@+id/textView2"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@+id/SizeButton1"
android:background="@drawable/button_border"
android:text="30mm"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="@+id/SizeButton3"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="@+id/textView2"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@+id/SizeButton2"
android:background="@drawable/button_border"
android:text="36mm"
android:textAllCaps="false"
android:textSize="12sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ImageGoods"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="size"
android:textColor="@color/Black"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/SizeButton3"
android:layout_marginTop="7dp">
<ExpandableListView
android:id="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:transcriptMode="disabled"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/colorFooter"
android:orientation="vertical" >
<Button
android:id="@+id/btn_add_goods_to_basket"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="5dp"
android:text="Add basket"
android:inputType="textFilter"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:textColor="@color/White" />
<Button
android:id="@+id/btn_change_qtty_minus"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_marginLeft="7dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:background="@drawable/plus_minus_button"
android:drawableTop="@drawable/minus"
android:gravity="center_horizontal"
android:padding="5dp"
android:textAlignment="center"
android:textColor="@color/White"
android:textSize="25dp" />
<Button
android:id="@+id/btn_change_qtty_pilus"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_marginStart="7dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:layout_toEndOf="@+id/textView5"
android:background="@drawable/plus_minus_button"
android:gravity="center_horizontal"
android:padding="5dp"
android:drawableTop="@drawable/pilus"
android:textAlignment="center"
android:textColor="@color/White"
android:textSize="25dp" />
<TextView
android:id="@+id/TotalSum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_change_qtty_minus"
android:layout_alignBottom="@+id/btn_change_qtty_minus"
android:layout_marginStart="7dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="@+id/btn_change_qtty_pilus"
android:textColor="@color/White"
android:gravity="center"
android:text="25$"
android:textStyle="bold" />
<TextView
android:id="@+id/textView5"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_change_qtty_minus"
android:layout_alignBottom="@+id/btn_change_qtty_minus"
android:layout_marginStart="7dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="@+id/btn_change_qtty_minus"
android:textStyle="bold"
android:textColor="@color/White"
android:gravity="center"
android:text="x1" />
</RelativeLayout>
</RelativeLayout>
这是我的 xml 代码。
如何解决这个问题?
刚刚找到我问题的答案。
@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;
}
private void setExpandableListViewHeight(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();
}