android listview固定分组,github源码分享PinnedSectionListView:分组的listView滑动中固定组标题的实现...

在很多应用中,看到这样的listview:listview滑动过程中分组标题固定在上方,当第二个组滑上来时,第一个组才跟着上滑,下一个组固定,直到该组也滑出上边缘。世上无难事只怕有心人,在github上就有人做出来了,而且效果很好(后来发现安卓自带应用中联系人应用就是这样的,估计github的作者也是仿照着联系人做出来的吧)。

先看截图:

d5853315647d492cbb4ba01ce2948124.png  

26941895d0f00f27b7a6434f8c093d0a.png  

8a2ec6816bd15d9c717fcba5b22cf879.png

PinnedSectionListView继承自listview,众所周知listview的每个子view都是按顺序跟着滚动的,要实现联系人listview的效果还真的找不到思路。看了PinnedSectionListView之后,感觉要改造一个现有的控件,一般都是通过重绘子view来实现的。ViewGroup(ListView继承自它)重绘子view的方法是dispatchDraw。

看看PinnedSectionListView在dispatchDraw中有那些特别的处理:@Override

protected void dispatchDraw(Canvas canvas) {

super.dispatchDraw(canvas);

if (mPinnedSection != null) {

// prepare variables

int pLeft = getListPaddingLeft();

int pTop = getListPaddingTop();

View view = mPinnedSection.view;

// draw child

canvas.save();

int clipHeight = view.getHeight() +

(mShadowDrawable == null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY));

canvas.clipRect(pLeft, pTop, pLeft + view.getWidth(), pTop + clipHeight);

canvas.translate(pLeft, pTop + mTranslateY);

drawChild(canvas, mPinnedSection.view, getDrawingTime());

if (mShadowDrawable != null && mSectionsDistanceY > 0) {

mShadowDrawable.setBounds(mPinnedSection.view.getLeft(),

mPinnedSection.view.getBottom(),

mPinnedSection.view.getRight(),

mPinnedSection.view.getBottom() + mShadowHeight);

mShadowDrawable.draw(canvas);

}

canvas.restore();

}

}

关键在于canvas.translate(pLeft, pTop + mTranslateY);意思是在绘制mPinnedSection的时候,listview滑动了多长的距离,就将canvas移动多少的距离,使mPinnedSection始终在可见的范围内固定不变。

使用方法:

1.在xml布局文件中将ListView替换成PinnedSectionListView

android:id="@android:id/list"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

2.让你的ListAdapter继承PinnedSectionListAdapter接口,最简单的做法是只增加isItemViewTypePinned方法,该方法必须在item为pinned的情况下返回true。// Our adapter class implements 'PinnedSectionListAdapter' interface

class MyPinnedSectionListAdapter extends BaseAdapter implements PinnedSectionListAdapter {

...

// We implement this method to return 'true' for all view types we want to pin

@Override

public boolean isItemViewTypePinned(int viewType) {

return viewType == ;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值