我们使用PullToRefresh库的时候,下拉的level-list以及刷新的帧动画都需要自己定制。如果每次都修改库非常麻烦,特别是一个项目有几个地方用到不同的样式,又不能多次关联同样的库。所以我们要自定义属性,在xml文件中就可以直接设置参数。
具体实现步骤:
1.在父类LoadingLayout中找到显示动画的ImageView控件,此控件自己定义,原来的控件隐藏。
mLoadingImage = (ImageView) mInnerLayout.findViewById(R.id.img_loading);
2.在attrs文件中
<declare-styleable name="PullToRefresh">
<attr name="ptrRefreshDrawable" format="reference" />
<attr name="ptrPullDrawable" format="reference" />
<attr name="ptrTotal" format="integer" />
3.// TODO在子类RotateLoadingLayout
refreshDrawable=attrs.getDrawable(R.styleable.PullToRefresh_ptrRefreshDrawable);
pullDrawable = attrs.getDrawable(R.styleable.PullToRefresh_ptrPullDrawable);
total = attrs.getInteger(R.styleable.PullToRefresh_ptrTotal, total);
4.// TODO 在onPullImpl方法中
int level = (int) (scaleOfLayout >= 1 ? total : scaleOfLayout * total);
if (pullDrawable == null)
{
mLoadingImage.setImageResource(R.drawable.homepage_pull_progresses);
} else
{
mLoadingImage.setImageDrawable(pullDrawable);
}
LevelListDrawable levelList = (LevelListDrawable) mLoadingImage.getDrawable();
levelList.setLevel(level);
5.// TODO 在refreshingImpl中
if (refreshDrawable == null)
{
mLoadingImage.setImageResource(R.drawable.homepage_loading_progressbar);
} else
{
mLoadingImage.setImageDrawable(refreshDrawable);
}
AnimationDrawable drawable = (AnimationDrawable) 6.mLoadingImage.getDrawable();
drawable.start();
// TODO 在resetImpl方法中
if (pullDrawable == null)
{
mLoadingImage.setImageResource(R.drawable.homepage_pull_progresses);
} else
{
mLoadingImage.setImageDrawable(pullDrawable);
}
7.在自己项目里关联库后
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:ptrPullDrawable="@drawable/homepage_pull_progresses"
app:ptrRefreshDrawable="@drawable/homepage_loading_progressbar"
app:ptrTotal="24" />
备注:参考附件上传的库里RotateLoadingLayout类和LoadingLayout类