[英]Expand the also the layout when the expandablelistview is expanded(inside a scrollview)
[英]Android ExpandableListView inside ScrollView not expand full height
我正在開發一個帶有左幻燈片菜單的Android應用,其中包含一些LinearLayout
和中間的ExpandableListview
,如圖所示
ExpandableListview
的子視圖在下面包含一個TextView
和一個ListView
,默認情況下,ListView的可見性消失了。 單擊TextView
,將顯示ListView
。
問題是,當單擊ExpandableListview
的父視圖時,列表的高度也會擴展以包裝其內容。 但是,當單擊子視圖(也為TextView
)時,列表的高度不會改變,我必須向下滾動才能看到最后一個項目。
要在ScrollView中設置ExpandableListview的高度,我使用了以下代碼:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if(listItem instanceof SecondLevelExpandableListView)
Log.e("abc", "SecondLevelExpandableListView");
if (listItem instanceof ViewGroup) {
Log.e("abc", "ViewGroup");
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
問題截圖
請幫助我解決此問題,我需要始終使用列表來包裝其內容。
提前致謝。
展開動畫結束時,請調用此命令:
listView.smoothScrollTo(x,y);
您可以使用listView.getScrollY-屏幕上的maxY找到y
Use the Snippet for ExpandableListView
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
public class ExpandableHeightListView extends ExpandableListView {
boolean expanded = false;
public ExpandableHeightListView(Context context) {
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
****
use <classpath for ExpandableHeightListView
attribs
/>
*****
ExpandableHeightListView expListView = (ExpandableHeightListView)findViewById(id);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.