Android简单版天气预报源码,下拉刷新(第三步)

接着上一步的操作,上一步已经能实现一个最简单的天气预报了
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篇写的不好,有时间会再更新

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值