首页 技术 正文
技术 2022年11月21日
0 收藏 503 点赞 3,854 浏览 1989 个字

项目源码

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

为什么做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()方法,注释以下代码

 

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

 

添加

  1. lv = new PinnedSectionListView(context, attrs);

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

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

 

  1. <com.handmark.pulltorefresh.library.PullToRefreshPinnedSectionListView xmlns:android=”http://schemas.android.com/apk/res/android”
  2. android:id=”@+id/list”
  3. android:layout_width=”match_parent”
  4. android:layout_height=”wrap_content”
  5. android:headerDividersEnabled=”false”
  6. android:footerDividersEnabled=”false”
  7. android:divider=”@null”
  8. />

 

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

 

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

 

即可通过

 

  1. ListView list = getListView();

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

另一种实现方式

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

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

建议

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848