«

Android ExpandableListView的使用技巧

时间:2024-3-2 18:09     作者:韩俊     分类: Android


Android ExpandableListView的使用技巧

转载请注明 作者:田野光 地址:http://blog.csdn.net/lovefish2/article/details/45242731

ExpandableListView是listview的官方扩展组件, 垂直方向上item分为两层, group item和child item,点击group item可以展开和折叠child item.基础的使用方法也非常简单. 与之对应的是BaseExpandableListAdapter. 这是只说一下一些常用需求的方法.

指定 默认展开的item

ExpandableListView vListView = (ExpandableListView) findViewById(R  .id.***);
vListView.expandGroup(0); //默认展开第一项

限制最多只展开一项child item

vListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            int previousGroup = 0;  //最初展开的item,若未指定,可设为-1

            @Override
            public void onGroupExpand(int groupPosition) {
                if (groupPosition != previousGroup)
                    vListView.collapseGroup(previousGroup);
                previousGroup = groupPosition;
            }
        });

与SwipeRefreshLayout搭配使用下拉刷新

我们知道, SwipeRefreshLayout 会监听滑动操作,并相应触发下拉刷新,如果布局文件中内含了可滑动的控件,如ScrollView等,则很有可能会在错误的时候触发下拉刷新(譬如当你下拉页面想要浏览上方内容), 好消息是所有AbsListView 子类,包括listview, ExpandableListView 等控件,只要是XML布局内部的直接下级,SwipeRefreshLayout都内置了判断方法,可以避免错误的触发.不过其他情况就需要单独处理了.

<SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

      <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</SwipeRefreshLayout>

非直接下级的AbsListView

参考(http://stackoverflow.com/questions/23341735/swiperefreshlayout-swipe-down-to-refresh-but-not-move-the-view-pull-down)

<SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

          <ExpandableListView
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    </LinearLayout>

</SwipeRefreshLayout>

解决办法:

listView.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    boolean enable = false;
    if(listView != null && listView.getChildCount() > 0){
        boolean firstItemVisible = listView.getFirstVisiblePosition() == 0;
        boolean topOfFirstItemVisible = listView.getChildAt(0).getTop() == 0;
        enable = firstItemVisible && topOfFirstItemVisible;
    }
    swipeRefreshLayout.setEnabled(enable);
}});

和RecyclerView

可以参考我的这篇博客: Android开发 解决RecyclerView in SwipeRefreshLayout触发下拉刷新的bug

增加item扩展和折叠的动画

ExpandableListView默认折叠和展开item简单粗暴,通常情况下我们想要平滑一点,轻柔一点(别想歪了-_-), 我们看到API里,官方有提供动画的方案

public boolean expandGroup (int groupPos, boolean animate)

但缺点是动画很粗糙,可以参见源码的处理:

AnimatedExpandableListView vListView = (AnimatedExpandableListView) findViewById(R  .id.***);
public boolean expandGroup(int groupPos, boolean animate) {
        ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
                ExpandableListPosition.GROUP, groupPos, -1, -1);
        PositionMetadata pm = mConnector.getFlattenedPos(elGroupPos);
        elGroupPos.recycle();
        boolean retValue = mConnector.expandGroup(pm);

        if (mOnGroupExpandListener != null) {
            mOnGroupExpandListener.onGroupExpand(groupPos);
        }

        if (animate) {
            final int groupFlatPos = pm.position.flatListPos;

            final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount();
            //动画仅仅是这样
            smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos),
                    shiftedGroupPosition);
        }
        pm.recycle();

        return retValue;
    }

而且API要求大于14.所以通常我们都是采用其他方式,这里我推荐AnimatedExpandableListView,一个第三方写的ExpandableListView类, 用法很简单,继承AnimatedExpandableListAdapter类,手动调用Animation方法

listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if (vBusInfo.isGroupExpanded(groupPosition)) {
                    vBusInfo.collapseGroupWithAnimation(groupPosition);
                } else {
                    vBusInfo.expandGroupWithAnimation(groupPosition);
                }
                return true;
            }

        });

好啦,关于ExpandableListView的介绍就先到这啦,欢迎大家提出批评建议,谢谢大家~

标签: android

热门推荐