接着上一步的操作,上一步已經能實現一個最簡單的天氣預報了
https://blog.csdn.net/weixin_44889138/article/details/102797849
源碼地址:https://github.com/LGH-cmd/android_weather.git
下面加入能夠刷新天氣預報的功能
刷新時獲得數據要有一定的時間,可以增加一個alertDialog,提醒用戶正在加載
其中**setView()**方法可以自定義一個view
首先實現簡單一個加載
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
點擊按鈕後去加載
btnX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View view1=LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_layout,null);
//顯示對話框
AlertDialog dialog=new Builder(MainActivity.this,R.style.CustomProgressDialog).create();
dialog.setView(view1);
dialog.show();
}
});
CustomProgressDialog
<style name="CustomProgressDialog" parent="Theme.AppCompat.Dialog">
<!--此屬性控制懸浮窗背景是否變暗-->
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
一個最簡單的加載就可以了
接着在項目中實現(優化加載,讓加載變得好看一點)
public class Utility {
private static AlertDialog mAlertDialog;
/**
* 彈出耗時對話框
* @param context
*/
public static void showProgressDialog(Context context) {
if (mAlertDialog == null) {
mAlertDialog = new AlertDialog.Builder(context, R.style.CustomProgressDialog).create();
}
View loadView = LayoutInflater.from(context).inflate(R.layout.circle_layout, null);
mAlertDialog.setView(loadView);
mAlertDialog.setCanceledOnTouchOutside(false);
TextView tvTip = loadView.findViewById(R.id.tvTip);
tvTip.setText("加載中...");
mAlertDialog.show();
}
/**
* 隱藏耗時對話框
*/
public static void dismiss() {
if (mAlertDialog != null && mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}
}
}
注:showProgressDialog()在未得到數據時調用,dismiss()在數據顯示出來後調用
還可以實現一個下拉刷新
導入依賴:implementation 'com.google.android.material:material:1.0.0’
這是谷歌爲了讓Android的下拉刷新風格能有一個統一的標準,制定的一個官方的設計規範
使用組件androidx.swiperefreshlayout.widget.SwipeRefreshLayout
之後調用setOnRefreshListener即可實現刷新
好了,更新到這,總共3篇寫的不好,有時間會再更新