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

项目源代码

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

为何作PullToRefresh-PinnedSection-ListView

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

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

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

    由于既有项目里的StickyListHeaders代码为1.x版本号,StickyListHeadersListView继承自ListView,故与handmark版的PullToRefreshListView作merge时很是顺畅;github

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

  • pinned-section-listviewspa

我是怎样作的

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

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

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

  
  
  
  
//        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。实现形式不一样,但效果相似,看过代码后实现起来也“至关顺畅”,有兴趣的同窗可以參照此项目。

建议

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

相关文章
相关标签/搜索