简体   繁体   English

具有可扩展页眉视图的ListView

[英]ListView with expandable Header view

I want to create a ListView with a header view that can be expanded and retractet just as in a ExpandableListView (but just the header view, all other views just be normal list items). 我想创建一个具有Header视图的ListView ,可以像ExpandableListView一样对其进行扩展和收缩(但仅仅是Header视图,所有其他视图只是普通列表项)。

I'm using an ExpandableListView with a BaseExpandableListAdapter and treat all the other items in the list as groups that have no children. 我使用带有BaseExpandableListAdapterExpandableListView ,并将列表中的所有其他项目视为没有子项的组。 This is cumbersome and error-prone and has unwanted sideeffects. 这麻烦且容易出错,并且具有不良的副作用。

I also tried using a normal ListView and a view for the header where I setVisivility(View.VISIBLE) to the expanded part of the view when the header view is clicked, but this doesn't seem to have any effect: 我还尝试使用普通的ListView和标题视图,单击标题视图时,将setVisivility(View.VISIBLE)设置为视图的扩展部分,但这似乎没有任何效果:

private View getHeaderView() {

    final View v = layoutInflater.inflate(R.layout.headertest, null);

    View v1 = v.findViewById(R.id.view1);

    final View v2 = v.findViewById(R.id.view2);

    v1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Log.d("head", "click");
            v2.setVisibility(View.VISIBLE);
            notifyDataSetChanged();

        }
    });

    return v;

}

When v1 is clicked, the visibility of v2 is not changed, although the clickListener is called. 单击v1 ,尽管调用clickListener ,但v2的可见性不会改变。 I also tried .invalidate() on all kinds of views, to no avail. 我也尝试在各种视图上使用.invalidate() ,但无济于事。

Any ideas on how to solve this one or the other way? 关于如何解决这一问题的任何想法?

You can do it with the expandable list view. 您可以使用可扩展列表视图进行操作。 First disable the indicators (open/close icons) on your ListView: 首先禁用ListView上的指示器(打开/关闭图标):

mExpList.setIndicator(null);

Next, your header (first group) will have to be a special type of group which has an image (an indicator) in its layout. 接下来,您的标题(第一组)必须是一种特殊类型的组,其布局中具有图像(指示器)。 Basically you want to have 2 types of group views: 基本上,您希望拥有两种类型的组视图:

@Override
public int getGroupTypeCount() {
    return 2;
}

@Override
public int getGroupType(int groupPosition) {
    if (groupPosition == headerPosition) {
        return 1;
    } else {
        return 0;
    }
}

You will also need to Override onGroupExpanded and onGroupCollapsed to change the image of your header (if a header was clicked of course). 您还需要覆盖onGroupExpandedonGroupCollapsed来更改标题的图像(当然,如果单击标题)。

Lastly you'll need to override the ListView's OnGroupClickListener: 最后,您需要覆盖ListView的OnGroupClickListener:

    mExpList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            if (groupPosition == header) {

                // Click was not handled
                return false;
            } else {
                // Do some normal stuff with items
                // Click was handled group won't be (or tried to be) expanded
                return true;
            }
        }
    });

Also a good idea would be to override adapters getChildCount to return 0 when it's not a header. 另外一个好主意是重写适配器getChildCount,使其不是标头时返回0。

Hope this helps. 希望这可以帮助。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM