Android应用中怎么实现一个下拉刷新功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
<强>一、下拉才出现的视图强>
<强> pull_to_refresh_header。xml 强>
& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android" android: id=癅 + id/pull_to_refresh_header" ,,,android:取向=皏ertical" ,,,android: layout_width=癿atch_parent" ,,,android: layout_height=皐rap_content" ,,,android:背景=? F3F3F3"比; & lt;使用 android:才能layout_width=癿atch_parent" android:才能layout_height=皐rap_content" android:才能paddingTop=?3 dp"比; & lt;才能LinearLayout android: id=癅 + id/pull_to_refresh_view" ,,android: layout_width=皐rap_content" ,,android: layout_height=皐rap_content" ,,android:取向=皏ertical" ,,android: layout_centerHorizontal=皌rue"比; & lt; TextView。才能 android: id=癅 + id/pull_to_refresh_text" ,,,android: layout_width=皐rap_content" ,,,android: layout_height=皐rap_content" ,,,android: layout_gravity=癱enter" android:文本=癅string/pull_to_refresh_text" ,,,android:输入textColor=? 777777“; ,,,android: textSize=?6 sp"/比; & lt; TextView。才能 android: id=癅 + id/pull_to_refresh_update_text" ,,,android: layout_width=皐rap_content" ,,,android: layout_height=皐rap_content" ,,,android: layout_gravity=癱enter" android:文本=癅string/pull_to_refresh_update_text" ,,,android:输入textColor=? 999999“; ,,,android: textSize=?4 sp"/比; & lt;才能/LinearLayout> & lt; ProgressBar才能 android: id=癅 + id/pull_to_refresh_progress" ,,android: layout_width=?0 dp" ,,android: layout_height=?0 dp" android: layout_toLeftOf=癅 id/pull_to_refresh_view" ,,android: layout_marginRight=?2 dp" ,,android: layout_marginTop=? dp" ,,android:不定=皌rue" android: indeterminateDrawable=癅drawable/ic_loading_refresh" ,,android:可见性=癵one"/比; & lt; ImageView才能 ,,android: id=癅 + id/pull_to_refresh_image" ,,android: layout_width=?2 dp" ,,android: layout_height=?2 dp" android: layout_toLeftOf=癅 id/pull_to_refresh_view" ,,android: layout_marginRight=?0 dp" ,,android: layout_marginTop=? dp" ,,android: src=https://www.yisu.com/zixun/癅drawable/ic_refresh_down” android: scaleType=" centerInside " android: contentDescription=" @string/app_name "/> <视图 android: layout_width=" match_parent " android: layout_height=" 15 dp "/>
<强> ic_loading_refresh。xml 强>
& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; rotate  xmlns: android=癶ttp://schemas.android.com/apk/res/android" ,android:可拉的=癅drawable/ic_loading" ,android: fromDegrees=?”; ,android: toDegrees=?60”; ,android: pivotX=?0%”; ,android: pivotY=?0%“,/在
1, ProgressBar的不定属性,代表进程的时间是否不确定。
2,黄色底的是android工作室的提示。第一个提示的是,当LinearLayout中的文本拓展得足够长时,会与ImageView重叠,实际效果是把ImageView给覆盖了第。二个是,建议用toStartOf代替toLeftOf,用marginEnd代替marginRight等。原因和影响还没完全搞懂。
<强>二,图标旋转的动画强>
private ImageView mPull_to_refresh_image; private RotateAnimation  mFlipAnimation; … mFlipAnimation 才能=,new RotateAnimation (0,, -180,, RotateAnimation.RELATIVE_TO_SELF,, 0.5度, ,,,RotateAnimation.RELATIVE_TO_SELF,, 0.5 f); mFlipAnimation.setInterpolator才能(new LinearInterpolator ()); mFlipAnimation.setDuration才能(250); mFlipAnimation.setFillAfter才能(真正的); … mPull_to_refresh_image.startAnimation (mFlipAnimation);
<强> 1,构造方法:强>
public RotateAnimation (float fromDegrees, float toDegrees,, int pivotXType,, float pivotXValue, ,,int pivotYType, float pivotYValue)
(1) toDegrees - fromDegrees & lt;null