实现原理:
将开源库中下拉刷新布局文件中的根节点FrameLayout visibility属性设置成GONE即可
在此方法中找到引用该布局的LoadingLayout文件,找到该FrameLayout,然后通过外部自定义标签动态设置其显隐状态
让我们以PullToRefreshScrollView为例,仅通过三步修改代码就实现上拉下拉的弹簧效果
第一步
打开res/vaules/attrs.xml,并在最后自定义如下属性:
<attr name="isElastic" format="reference|boolean" />
第二步
找到com.handmark.pulltorefresh.internal包下的LoadingLayout.java文件。
先声明一个布尔型的变量 private boolean IsElastic;
然后找到如下代码段
mInnerLayout = (FrameLayout) findViewById(R.id.fl_inner);
mHeaderText = (TextView) mInnerLayout.findViewById(R.id.pull_to_refresh_text);
mHeaderProgress = (ProgressBar) mInnerLayout.findViewById(R.id.pull_to_refresh_progress);
mSubHeaderText = (TextView) mInnerLayout.findViewById(R.id.pull_to_refresh_sub_text);
mHeaderImage = (ImageView) mInnerLayout.findViewById(R.id.pull_to_refresh_image);
在这后面加上下列代码
if (attrs.hasValue(R.styleable.PullToRefresh_isElastic)) {
IsElastic = attrs.getBoolean(R.styleable.PullToRefresh_isElastic, false);
}
if(IsElastic)
mInnerLayout.setVisibility(View.GONE);
第三步
在调用PullToRefreshScrollView的布局文件里设置isElastic属性,类似下面:
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrAnimationStyle="flip"
ptr:ptrMode="both"
ptr:isElastic="true">
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
完毕!