基本上我需要通過適配器動態提供一些內容來填充我的expandableListView的標題,這一定是可能的,但它是一個非常困難在這個問題上找到任何資源。是否有可能在一個expandable的標題中的項目列表視圖
下面是我期待做的一個表示。
基本上我需要通過適配器動態提供一些內容來填充我的expandableListView的標題,這一定是可能的,但它是一個非常困難在這個問題上找到任何資源。是否有可能在一個expandable的標題中的項目列表視圖
下面是我期待做的一個表示。
是的,但你需要重寫onGlobalLayout(),並在頭例如設置爲ListView的高度
final ExpandableListView lExpView = (ExpandableListView) view.findViewById(R.id.expandable_list);
View viewHdr = getActivity().getLayoutInflater().inflate(R.layout.pager_header, lExpView, false);
final ListView lView = (ListView) viewHdr.findViewById(R.id.item_hdr_list);
...
lView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (lView.getVisibility() == View.VISIBLE) adjustTotalHeightOfListView(lView, lExpView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
lExpView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
lExpView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
凡adjustTotalHeightOfListView()是你自己的設計,可以計算並設置的ListView的高度的方法,而包含在可擴展列表的標題視圖。如果listView中的單個項目的高度可能超過一個文本行(寬於可擴展列表視圖寬度,並且您有WRAP_CONTENT集),則會變得有點複雜,因爲包含ExpandableListView的實際寬度不會是可用,直到它被呈現,但我們至少需要一個猜測才能讓它發生。所以,你會想一些代碼,看起來有點像(未測試):
if (listView == null) return;
ListAdapter mAdapter = listView.getAdapter();
if (mAdapter == null) return;
int totalHeight = listView.getPaddingBottom() + listView.getPaddingTop();
int viewWidth = (parent == null || parent.getWidth() <= 0)
? listView.getWidth()
: parent.getWidth();
....
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(viewWidth, (viewWidth > 0)
? View.MeasureSpec.AT_MOST
: View.MeasureSpec.UNSPECIFIED
),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
totalHeight += mView.getMeasuredHeight() + listView.getDividerHeight();
}
....
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
listView.requestLayout();
注:? View.MeasureSpec.AT_MOST:View.MeasureSpec.UNSPECIFIED在measure()語句中破解,本質上它是在父視圖展開/設置了寬度之前應對所做的調用。如果寬度已設置不超過它,所以如果指定了WRAP_CONTENT,並且寬度大於父寬度,則會調整高度,否則假定您可以與內容一樣寬。
其中:pager_header.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ListView
android:id="@+id/item_hdr_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</GridLayout>
和主要佈局包含:
<?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"
android:gravity="center">
<ExpandableListView
android:id="@+id/expandable_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:paddingBottom="10dp" />
</LinearLayout>
不容易,想的是自己這是更好/更容易使用'LinearLayout'在後頭部而不是'ListView'。 –
其實我很久以前就已經解決了這個問題,而且應該已經更新了,你最好的選擇是使用一個ListView來填充你知道大小的項目。然後獲取X的高度,並以編程方式將該高度設置爲ListView,從而不滾動。 – Derek