使用“动画圆圈”加载内容时在 ImageView 中
我目前在我的应用程序中使用一个列表视图,可能需要一秒钟才能显示。
我目前所做的是使用列表视图的 @id/android:empty 属性来创建“正在加载”文本。
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="Loading..."/>
现在,我想将其替换为加载对话框中使用的动画圆圈而不是此文本,我想你们都知道我的意思:
编辑:我不需要对话框。我想在我的布局中展示这一点。
非常感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需将这个 xml 块放入您的 Activity 布局文件中:
当您完成加载时,调用这一行:
结果(它也会旋转):
Simply put this block of xml in your activity layout file:
And when you finish loading, call this one line:
The result (and it spins too):
您可以通过使用以下 xml 来实现此目的
使用此样式
要使用此样式,您必须通过将可见性值设置为 GONE 来隐藏 UI 元素,并且每当加载数据时,调用
setVisibility(View.VISIBLE)
恢复您所有的观点。不要忘记调用findViewById(R.id.loadingPanel).setVisiblity(View.GONE)
来隐藏加载动画。如果您没有加载事件/功能,但只想加载面板在 x 秒后消失,请使用 句柄触发隐藏/显示。
You can do this by using the following xml
With this style
To use this, you must hide your UI elements by setting the visibility value to GONE and whenever the data is loaded, call
setVisibility(View.VISIBLE)
on all your views to restore them. Don't forget to callfindViewById(R.id.loadingPanel).setVisiblity(View.GONE)
to hide the loading animation.If you dont have a loading event/function but just want the loading panel to disappear after x seconds use a Handle to trigger the hiding/showing.
这通常称为不确定进度栏或不确定进度对话框。
将此与 Thread 和 Handler 以获得您想要的。有很多关于如何通过 Google 或此处执行此操作的示例。我强烈建议花时间学习如何使用这些类的组合来执行这样的任务。它在许多类型的应用程序中都非常有用,并且可以让您深入了解线程和处理程序如何协同工作。
我将向您介绍这是如何工作的:
加载事件启动对话框:
现在线程执行工作:
最后在完成时从线程返回状态:
This is generally referred to as an Indeterminate Progress Bar or Indeterminate Progress Dialog.
Combine this with a Thread and a Handler to get exactly what you want. There are a number of examples on how to do this via Google or right here on SO. I would highly recommend spending the time to learn how to use this combination of classes to perform a task like this. It is incredibly useful across many types of applications and will give you a great insight into how Threads and Handlers can work together.
I'll get you started on how this works:
The loading event starts the dialog:
Now the thread does the work:
Finally get the state back from the thread when it is complete:
您可以使用 firebase github 示例 ..
您不需要在布局文件中进行编辑...只需
在您想要使用进度的 Activity 中 创建一个新类“BaseActivity”对话框..
在需要时间的功能之前/之后
You can use this code from firebase github samples ..
You don't need to edit in layout files ... just make a new class "BaseActivity"
In your Activity that you want to use the progress dialog ..
Before/After the function that take time
如果您不想仅仅为了指示进度而膨胀另一个视图,请执行以下操作:
Android 将照顾进度条的可见性。
例如,在
activity_main.xml
中:在
MainActivity.java
中:}
If you would like to not inflate another view just to indicate progress then do the following:
Android will take care the progress bar's visibility.
For example, in
activity_main.xml
:And in
MainActivity.java
:}
对于用 Kotlin 开发的,有一个 Anko 库提供的甜蜜方法,使显示
ProgressDialog
的过程变得轻而易举!基于该链接:
这将显示一个进度对话框,其中显示进度%(为此您还必须传递
init
参数来计算进度)。还有
inminatedProgressDialog()
方法,它无限期地提供旋转圆圈动画,直到关闭:大声喊出 这个博客引导我找到了这个解决方案。
For the ones developing in Kotlin, there is a sweet method provided by the Anko library that makes the process of displaying a
ProgressDialog
a breeze!Based on that link:
This will show a Progress Dialog with the progress % displayed (for which you have to pass the
init
parameter also to calculate the progress).There is also the
indeterminateProgressDialog()
method, which provides the Spinning Circle animation indefinitely until dismissed:Shout out to this blog which led me to this solution.