Android开发 - 下拉刷新和分段头悬停列表

项目源代码

本文所述项目已开源,源代码地址

为什么做PullToRefresh-PinnedSection-ListView

前段时间由于项目需求,须要在Android中对ListView同一时候添加下拉刷新和分段头悬停的效果。受到dkmeteor的启示,Merge了两个Github上的开源项目:

  • Android-PullToRefresh(handmark版,眼下已不再更新)

  • StickyListHeaders(眼下版本号为2.x)

    因为既有项目里的StickyListHeaders代码为1.x版本号,StickyListHeadersListView继承自ListView,故与handmark版的PullToRefreshListView做merge时非常顺畅;

    但2.x版的StickyListHeadersListView继承自FrameLayout,与PullToRefresh的融合并不顺利,若要整理拆分出一个独立的lib时遇到非常多的问题,故在分断头悬停需求上採用了还有一个类似的开源项目:

  • pinned-section-listview

我是怎样做的

前面已经介绍过这个过程是“非常顺畅”的:

1.Library方面,基于PullToRefresh的Library改动,首先使其依赖StickyListHeaders的Library,通过拷贝src/com/handmark/pulltorefresh/library/PullToRefreshListView.java类。新建PullToRefreshPinnedSectionListView.java类;

2.改动PullToRefreshPinnedSectionListView类中createListView()方法,凝视下面代码

//        if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD) {
//            lv = new InternalListViewSDK9(context, attrs);
//        } else {
//            lv = new InternalListView(context, attrs);
//        }

加入

lv = new PinnedSectionListView(context, attrs);

3.Example方面,基于pinned-section-listview的example改动,令其依赖PullToRefresh的Library;

4.改动主类资源文件activity_main.xml,设置List组件为新类:

<com.handmark.pulltorefresh.library.PullToRefreshPinnedSectionListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:divider="@null"
/>

5.改写SampleActivity.java类中getListView()方法:

mpPullToRefreshPinnedSectionListView = (PullToRefreshPinnedSectionListView) findViewById(R.id.list);
return mpPullToRefreshPinnedSectionListView.getRefreshableView();

就可以通过

ListView list = getListView();

继续进行原example其它操作,详情可阅读项目代码

还有一种实现方式

本例的实现方式依赖于handmark版下拉刷新组件的灵活性,更重要的一点,要求分段头悬停组件是继承自ListView实现;故同理也可用handmark版下拉刷新组件和1.x版的StickyListHeaders组件实现;

还有一种实现方式为pull-to-refresh-sticky-list,其採取合并的是2.x版的StickyListHeaders和johannilsson的android-pulltorefresh。实现形式不同,但效果类似,看过代码后实现起来也“相当顺畅”,有兴趣的同学能够參照此项目。

建议

建议在熟悉或使用过原有组件类库的前提下使用本类库。

posted @ 2017-04-15 09:52  yangykaifa  阅读(1328)  评论(0编辑  收藏  举报