Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

android - Is it possible to have a listView of items in the header of an expandableListView

Basically I need to have some content that is dynamically available through an adapter to populate the header of my expandableListView, this must be possible but it's been a really hard time finding any resources on this issue.

Here is a representation of what I'm looking to do.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, but you'll need to override onGlobalLayout(), and set a height for the listView in the header e.g.

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);
        }
    });

Where adjustTotalHeightOfListView() is a method of your own devising that can calculate and set the height of your listView, while contained in the header of the expandable list view. If the individual items in the listView can be more than one Text line in height (wider than the expandable list views width and you have WRAP_CONTENT set) this get's a bit complex, as the actual width of the containing ExpandableListView won't be available till it's rendered, but we need at least a guess before to allow this to happen. So you'll want some code that looks a bit like (not tested):

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();

Note: The ? View.MeasureSpec.AT_MOST : View.MeasureSpec.UNSPECIFIED hack in the measure() statement, essentially it's there to cope with a call being made before the parent view has been expanded / had a width set. If a width has been set don't exceed it, so if WRAP_CONTENT is specified and the width is greater than the parent the height will be adjusted, else assume you can be as wide as the content.

Where: 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>

and the main layout contains:

<?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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...