溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android應用中怎么實現一個下拉刷新功能

發布時間:2020-11-26 15:16:18 來源:億速云 閱讀:206 作者:Leah 欄目:移動開發

Android應用中怎么實現一個下拉刷新功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

一、下拉才出現的視圖

pull_to_refresh_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pull_to_refresh_header"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F3F3F3">
 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:paddingTop="23dp">
  <LinearLayout 
       android:id="@+id/pull_to_refresh_view"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:layout_centerHorizontal="true">
   <TextView
android:id="@+id/pull_to_refresh_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
android:text="@string/pull_to_refresh_text"
    android:textColor="#777777"
    android:textSize="16sp"/>
   <TextView
android:id="@+id/pull_to_refresh_update_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
android:text="@string/pull_to_refresh_update_text"
    android:textColor="#999999"
    android:textSize="14sp"/>
  </LinearLayout>
  <ProgressBar
android:id="@+id/pull_to_refresh_progress"
   android:layout_width="30dp"
   android:layout_height="30dp"
android:layout_toLeftOf="@id/pull_to_refresh_view"
   android:layout_marginRight="22dp"
   android:layout_marginTop="5dp"
   android:indeterminate="true"
android:indeterminateDrawable="@drawable/ic_loading_refresh"
   android:visibility="gone"/>
  <ImageView
   android:id="@+id/pull_to_refresh_image"
   android:layout_width="32dp"
   android:layout_height="32dp"
android:layout_toLeftOf="@id/pull_to_refresh_view"
   android:layout_marginRight="20dp"
   android:layout_marginTop="5dp"
   android:src="@drawable/ic_refresh_down"
   android:scaleType="centerInside"
android:contentDescription="@string/app_name"/>
 </RelativeLayout>
 <View
  android:layout_width="match_parent"
  android:layout_height="15dp"/>
</LinearLayout>

ic_loading_refresh.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
 android:drawable="@drawable/ic_loading"
 android:fromDegrees="0"
 android:toDegrees="360"
 android:pivotX="50%"
 android:pivotY="50%" />

1、ProgressBar的indeterminate屬性,代表進程的時間是否不確定。

2、黃色底的是Android Studio的提示。第一個提示的是,當LinearLayout中的Text拓展得足夠長時,會與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.5f,
    RotateAnimation.RELATIVE_TO_SELF, 0.5f);
  mFlipAnimation.setInterpolator(new LinearInterpolator());
  mFlipAnimation.setDuration(250);
  mFlipAnimation.setFillAfter(true);

...
mPull_to_refresh_image.startAnimation(mFlipAnimation);

1、構造方法:

public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
   int pivotYType, float pivotYValue)

(1)toDegrees - fromDegrees < 0 就會是逆時針旋轉,反之是順時針。這是我取不同值測試出來的。水平線右邊是0°,或者360°,左邊是180°,或者是-180°。

(2)pivotType是樞軸類型,也就是旋轉中心。RELATIVE_TO_SELF代表相對這個view本身。0.5f代表這個view一半大小的位置。

2、setInterpolator作用是設置速度器。LinearInterpolator是勻速加速器,也就是勻速播放動畫。

3、setDuration作用是設置動畫時長,以毫秒為單位。

4、setFillAfter作用是,當參數為true時,動畫播放完后,view會維持在最終的狀態。而默認值是false,也就是動畫播放完后,view會恢復原來的狀態。

關于Android應用中怎么實現一個下拉刷新功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女