(一) 开发当中我们用到PullToRefreshListView 来做刷新和加载功能,前奏基本上都知道,添加依赖就不多说了,
我主要说的是自定义加载动画和上拉加载更多只显示文字不现实动画这些东西,下面开始吧。
1.
// 设置上拉刷新文本
listview.getLoadingLayoutProxy(false, true)
.setPullLabel("加载更多...");
listview.getLoadingLayoutProxy(false, true).setReleaseLabel(
"放开加载...");
listview.getLoadingLayoutProxy(false, true).setRefreshingLabel(
"正在加载...");
// 设置下拉刷新文本
listview.getLoadingLayoutProxy(true, false)
.setPullLabel("下拉刷新数据");
listview.getLoadingLayoutProxy(true, false).setReleaseLabel(
"释放即可刷新");
listview.getLoadingLayoutProxy(true, false).setRefreshingLabel(
"努力加载中...");
不难发现上拉和下拉的样式是通过true和false来控制的,这个时候如果我们想让上拉加载更多不显示动画只显示文字,可以这样写/
listview.getLoadingLayoutProxy(false,true).setLoadingDrawable(null);
如果下拉刷新也不想要动画,只要把true和false改变一下位置就可以了。2.设置自定义动画,假如我们需要自己设定一个帧动画来显示,首先要在RefreshLibrary\src\main\res\drawable\caranimation.xml路径下设置一个帧动画如下,
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/refresh_car" android:duration="150" />
<item android:drawable="@mipmap/refreshcar_t" android:duration="150" />
</animation-list>
然后在pulltorefresh\library\internal\RotateLoadingLayout.java 的类中修改一下代码,如下;
@Override
protected int getDefaultDrawableResId() {
return R.drawable.caranimation;
}
《最近公司提了个分页加载的需求,本想着很简单,但是上拉加载过后页面一直置顶,
不能显示新更新的数据,找了很多办法,用过PullTolist.setSelection();都不行,
最后发现是因为PullToList.setAdapter()这个用了两次,
上拉的时候不能用PullToList.setAdapter(adapter);不然真的会一直置顶的。》
《
//进入页面就呈现下拉状态
handler.postDelayed(new Runnable() {
@Override
public void run() {
PullToRefresh.setRefreshing(true);
}
},3000);
//PullToRefresh不能直接使用setSelection();这个方法,必须要先找到listvieru如下;
PullToRefresh.getRefreshableView().setSelection();
》
-------------------------------------------------------------------------------------
第一步:
源码分析:
PullToRefrehListView 默认加载动画是很难看的:
默认是很难看的 但我们想要实现我们的效果怎么办?
分析源码:
找到PullRefreshListView 分析:
我们知道 上拉和下拉加载 动画无非是 pullToRefreshListView 中添加了头和脚, 而头和脚都是动画!!
PullToRefreshListView.Java 两个动画类变量:
//定义 头部和尾部的加载动画
private LoadingLayout mHeaderLoadingView;
private LoadingLayout mFooterLoadingView;
...
//加载布局
@Override
protected LoadingLayoutProxy createLoadingLayoutProxy(final boolean includeStart, final boolean includeEnd) {
LoadingLayoutProxy proxy = super.createLoadingLayoutProxy(includeStart, includeEnd);
if (mListViewExtrasEnabled) {
final Mode mode = getMode();
if (includeStart && mode.showHeaderLoadingLayout()) {
//添加 头部动画 到listView中
proxy.addLayout(mHeaderLoadingView);
}
if (includeEnd && mode.showFooterLoadingLayout()) {
//添加 添加脚部动画 到listView中
proxy.addLayout(mFooterLoadingView);
}
}
return proxy;
}
...
createLoadingLayoutProxy方法继承自抽象类PullToRefreshAdapterViewBase查看方法没有该方法继续父类查找,查找PullToRefreshAdapterViewBase父类PullToRefreshBase:
// We need to create now layouts now
mHeaderLayout = createLoadingLayout(context, Mode.PULL_FROM_START, a);
mFooterLayout = createLoadingLayout(context, Mode.PULL_FROM_END, a);
...
//加载动画 布局----------
protected LoadingLayout createLoadingLayout(Context context, Mode mode, TypedArray attrs) {
LoadingLayout layout = mLoadingAnimationStyle.createLoadingLayout(context, mode,
getPullToRefreshScrollDirection(), attrs);
layout.setVisibility(View.INVISIBLE);
return layout;
}
...
//此处实现了我们需要 修改的动画方法:
//修改代码实现 自己的下载刷新动画
LoadingLayout createLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
switch (this) {
case ROTATE://旋转动画
default:
//帧动画 frameAnimationLayout为 自定义动画类
return new FrameAnimationLayout(context, mode, scrollDirection, attrs);
//旋转动画 默认的动画
// return new RotateLoadingLayout(context, mode, scrollDirection, attrs);
case FLIP:
return new FlipLoadingLayout(context, mode, scrollDirection, attrs);
}
//自定义动画类 实现动画
package com.handmark.pulltorefresh.library.internal;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.R;
/**
* Package_name:com.handmark.pulltorefresh.library.internal
* Author:zhaoQiang
* Email:zhao_hero@163.com
* Date:2016/11/28 19:34
*
* 帧动画 实现加载自定义的动画 实现的是帧动画
*/
public class FrameAnimationLayout extends LoadingLayout{
//继承自 PullToRefreshListView提供的loadingLayout类
private AnimationDrawable mAnimationDrawable;
public FrameAnimationLayout(Context context, PullToRefreshBase.Mode mode,
PullToRefreshBase.Orientation scrollDirection, TypedArray attrs) {
super(context, mode, scrollDirection, attrs);
mHeaderImage.setImageResource(R.drawable.ptr_animation);
mAnimationDrawable = (AnimationDrawable) mHeaderImage.getDrawable();
}
@Override
protected int getDefaultDrawableResId() {
//返回 自定义动画布局
return R.drawable.ptr_animation;
}
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
}
@Override
protected void onPullImpl(float scaleOfLayout) {
}
@Override
protected void pullToRefreshImpl() {
}
//刷新的时候
@Override
protected void refreshingImpl() {
mAnimationDrawable.start();//开启动画
}
@Override
protected void releaseToRefreshImpl() {
}
@Override
protected void resetImpl() {
}
}
anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/ptr_img_0" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_1" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_2" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_3" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_4" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_5" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_6" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_7" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_8" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_9" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_10" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_11" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_12" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_13" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_14" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_15" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_16" android:duration="10"></item>
<item android:drawable="@drawable/ptr_img_17" android:duration="10"></item>
</animation-list>