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

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

问题描述

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

推荐答案

是的,但是你需要重写onGlobalLayout(),并在header中为listView设置一个高度,例如

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),则这有点复杂,因为包含的实际宽度ExpandableListView 在它被渲染之前将不可用,但我们至少需要一个猜测才能允许这种情况发生.所以你会想要一些看起来有点像(未经测试)的代码:

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 :measure() 语句中的 View.MeasureSpec.UNSPECIFIED hack,本质上它是为了处理在父视图展开/具有宽度设置.如果设置了宽度不要超过它,所以如果指定了 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>

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

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