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。實現形式不一樣,但效果相似,看過代碼後實現起來也「至關順暢」,有興趣的同窗可以參照此項目。

建議

建議在熟悉或使用過原有組件類庫的前提下使用本類庫。

相關文章
相關標籤/搜索