是否有可能有一个项目一个ListView在expandableListView的头 [英] Is it possible to have a listView of items in the header of an expandableListView

查看:137
本文介绍了是否有可能有一个项目一个ListView在expandableListView的头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我需要有一些内容是动态可通过一个适配器来填充我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.

下面是什么,我希望做一个重新presentation。

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

推荐答案

是的,但你需要重写 onGlobalLayout(),并设置一个高度的头,例如

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

其中 adjustTotalHeightOfListView()是自己的设计,可以计算并设置的ListView的高度,在可扩展列表视图的标题,同时含有的方法。如果在ListView的各个项目可以在高度多个文本行(宽于可扩展列表视图的宽度和你的 WRAP_CONTENT 集)本GET有点复杂,因为包含的实际宽度ExpandableListView将无法使用,直到它的渲染,但我们至少需要一个猜测之前,允许这种情况发生。所以,你会想一些code,看起来有点像(未测试):

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

注:? View.MeasureSpec.AT_MOST:View.MeasureSpec.UNSPECIFIED 劈在措施()语句,实际上它的存在,以应付呼叫正在前父视图已经扩展/有宽度集。如果一个宽度已经设置不超​​过它,所以如果指定WRAP_CONTENT且宽度大于父的高度将被调整,否则假定可以一样宽的内容。

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.

其中: 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>

这篇关于是否有可能有一个项目一个ListView在expandableListView的头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆