9

Android自动展示和关闭进度条

 3 years ago
source link: https://jiajunhuang.com/articles/2021_01_27-android_progress_bar.md.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Android自动展示和关闭进度条

客户端总是要有个进度条告诉用户,正在加载内容。可是我很懒,不想每次都自己来控制,那样子的话,得在调用api前设置 进度条显示出来,调用api之后不显示。每次都要这样,太麻烦了。

那么怎么偷懒呢?就是用一个trick,把每个布局文件里的progressbar的id都设置成 progress_bar,然后:

private fun showProgressBar() {
    runOnUiThread {
        findViewById<ProgressBar>(R.id.progress_bar)?.let {
            it.isVisible = true
        }
    }
}

private fun hideProgressBar() {
    runOnUiThread {
        findViewById<ProgressBar>(R.id.progress_bar)?.let {
            it.isVisible = false
        }
    }
}

// 调用gRPC函数的外层处理
fun callRPC(func: () -> Any): Any? {
    try {
        showProgressBar()
        return func()
    } catch (e: StatusRuntimeException) {
        Log.e(TAG, "callgRPC: failed with %s", e)
        showSnackBar(e.status.description.toString())
    } finally {
        hideProgressBar()
    }

    return null
}

duang,搞定。如果有些地方不想在调用api之前显示进度条,那可以改一改 callRPC 这个函数加入一个参数来控制。

不过,这篇就到这里吧。


微信公众号
关注公众号,获得及时更新

Python中的并发控制

KVM spice协议在高分屏上的分辨率问题

计算机中的权衡(trade-off)

[声明]本站所有文章禁止转载

Golang不那么蛋疼的sort

Flutter给Android应用签名

使用Gitea+Drone打造自己的CI/CD系统

2019年就要结束啦!

为什么要使用gRPC?

Matebook X Pro 2019安装Debian 10

ArchLinux忽略某个包的升级

SQLAlchemy使用主从与数据库autocommit

Blackbox禁用IPv6

Go 1.13的errors挺香

预防缓存击穿




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK