上拉加载下拉刷新控件,在网上一搜一大把,看了很多,但都不能达到我想要的结果,因此总结网友的优秀代码,实现了一个能够灵活自定义、大众化、简洁实用的加载控件。(适用于多种View)
注:因为代码较多,不方便直接贴出,有需要看源码的伙伴点击下载源码,源码有惊喜哦,(__) 嘻嘻……**
<h4>**一、需求 **</h4>
需求很简单,就是实现通用的大众化上拉加载,下拉刷新
<h4>二、效果展示 </h4>
![](https://img.haomeiwen.com/i3143023/5d9fb60f8c31a023.gif)
<h4>三、依赖</h4>
//第一步
allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
}
//第二步
compile 'com.github.hsy815:RefreshPullLoad:v1.2'
<h4>**四、使用 **</h4>
注:因源码有逐步详细注释,在此就不贴出源码解释了,感兴趣的朋友可以下载源码阅读,下面我就贴出具体的使用代码
1、在xml中添加如下:
<com.hsy.pdload.view.OrdinaryPDLView
android:id="@+id/pullDownLoadView"
android:layout_width="match_parent"
android:layout_height="match_parent">
//这里是你自己的View
<com.hsy.pdload.view.MyRecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.hsy.pdload.view.OrdinaryPDLView>
2、Java中如下:
//(普通加载View)
ordinaryPDLView =(OrdinaryPDLView)findViewById(R.id.pullDownLoadView);
//刷新、上拉加载监听
ordinaryPDLView.setOnRefreshListener(new OrdinaryPDLView.onRefreshListener() {
@Override
public void onRefresh() {
//在此做刷新处理
}
@Override
public void onLoadMore() {
//在此做加载处理
}
});
3、可选方法:
//头部下拉滑动距离 默认是头部View的高度、 ordinaryPDLView.MoveDistanceAll全屏、 ordinaryPDLView.MoveDistanceIn 半屏
ordinaryPDLView.setMoveDistanceTop(ordinaryPDLView.MoveDistanceAll);
//底部上拉滑动距离 默认是底部View的高度、 ordinaryPDLView.MoveDistanceAll全屏、 ordinaryPDLView.MoveDistanceIn 半屏
ordinaryPDLView.setMoveDistanceBtn(ordinaryPDLView.MoveDistanceAll);
//添加头部拖动箭头
ordinaryPDLView.setDrawable_h(R.mipmap.down);
//添加底部拖动箭头
ordinaryPDLView.setDrawable_f(R.mipmap.pulls);
//头部加载时文本View、文本内容以及属性自己设置
ordinaryPDLView.ordinary_pdl_tv_h.setText("正在加载");
//底部加载时文本View、文本内容以及属性自己设置
ordinaryPDLView.ordinary_pdl_tv_f.setText("正在加载");
//设置加载文本本内容
PDLSetting pdlSetting = new PDLSetting("松开刷新", "松开加载", "继续向下拉", "继续向上拉");
ordinaryPDLView.setPdlSetting(pdlSetting);
//头部加载时旋转的图片
ordinaryPDLView.ordinary_pdl_load_h.setImageResource(R.mipmap.loading_dian);
//底部加载时旋转的图片
ordinaryPDLView.ordinary_pdl_load_f.setImageResource(R.mipmap.loading);
//设置头部背景
ordinaryPDLView.LayoutHeader.setBackgroundResource(R.color.bg);
//设置底部背景
ordinaryPDLView.LayoutFooter.setBackgroundResource(R.color.bg);
//旋转动画
Animation operatingAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.ordinary_anim);
LinearInterpolator lin = new LinearInterpolator();
operatingAnim.setInterpolator(lin);
//添加旋转动画
ordinaryPDLView.setOperatingAnim(operatingAnim);
<h4>五、点击源码下载</h4>
以上就是笔者给大家分享的上拉加载下拉刷新控件,可能也不是万能的,但绝对大众化,O(∩_∩)O谢谢各位的支持!
网友评论