Android之带之带group指示器的指示器的ExpandableListView(自写自写)
我们都知道Android缺省的ExpandableListView的group header无法固定在界面上,当向下滚动后,不能对当前显示的那些
child 指示出它们归属于哪个group,在网上搜了很多关于仿手机QQ好友分组效果的ExpandableListView,发现都不尽如意,
于是乎在别人的基础上改进了一点点,其实原理还是差不多的,只是增加了往上挤出去的动画效果,而且更加简单,只不过还
是没有完全到达跟QQ一样的效果,希望有高手能实现更加逼真的效果,下面我们先看看效果图:
我这里没有把ExpandableListView独立出来形成一个新的控件,跟网上很多朋友一样,监听OnScrollListener事件,当group不
是在第一个位置时,就把我们头部的那个indicator显示出来,并且让它的view跟当前child所在group的view一样的,然后再增
加一个点击关闭组的事件,即达到了简单的仿QQ好友分组的效果。
下面我们先来看看主要的布局文件:main.xml,下面那个topGroup的FrameLayout就是我们的指示器。
代码如下:
<?xml version=”1.0″ encoding=”utf-8″?>
<FrameLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >
<ExpandableListView
android:id=”@+id/expandableListView”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >
</ExpandableListView>
<FrameLayout
android:id=”@+id/topGroup”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical” >
</FrameLayout>
</FrameLayout>
其中最重要的是下面这部分:我们监听onSroll这个接口事件,当ListView在滑动时,做相应的处理,代码中注释比较多,我这
里就不多作说明了。
代码如下:
/**
* here is very importance for indicator group
*/
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
final ExpandableListView listView = (ExpandableListView) view;
/**
* calculate point (0,0)
*/
int npos = view.pointToPosition(0, 0);// 其实就是firstVisibleItem
if (npos == AdapterView.INVALID_POSITION)// 如果第一个位置值无效
return;
long pos = listView.getExpandableListPosition(npos);
int childPos = ExpandableListView.getPackedPositionChild(pos);// 获取第一行child的id
int groupPos = ExpandableListView.getPackedPositionGroup(pos);// 获取第一行group的id
if (childPos == AdapterView.INVALID_POSITION) {// 第一行不是显示child,就是group,此时没必要显示指示器
评论0
最新资源