[英]Wait until a View is loaded and then put loaded data in it
I have this small problem. 我有这个小问题。 My custom View
takes to load from 100 ms to 1 s depending on how fast is the phone. 我的自定义View
加载时间从100毫秒到1 s,具体取决于手机的速度。 The view is in Fragment
but I'm loading data in the MainActivity
. 该视图位于Fragment
但我正在MainActivity
加载数据。 Sometimes the data is loaded faster than the View
in Fragment
which happens to be blank since the data is not present in the View. 有时,数据加载的速度比“ Fragment
View
快,因为“ View
中不存在数据,因此恰好为空白。
How can I delay data input into the View
? 如何延迟将数据输入到View
?
There are several ways to do that. 有几种方法可以做到这一点。 Personally I would like to suggest you to use ProgressFragment
for these purpose. 我个人建议您将ProgressFragment
用于这些目的。 Here's a nice library to show a default progress animation when the data is still being loaded. 这是一个不错的库,可以在仍在加载数据时显示默认的进度动画。
The implementation of the library is quite simple. 该库的实现非常简单。
Add this in your gradle
dependancies. 将此添加到您的gradle
依赖关系中。
dependencies {
compile 'com.github.johnkil.android-progressfragment:progressfragment:1.4.+'
}
Now your Fragment
should extend the ProgressFragment
现在,您的Fragment
应该扩展ProgressFragment
public class MyProgressFragment extends ProgressFragment {
// your code of fragment
}
Setup content view in onActivityCreate()
method. 在onActivityCreate()
方法中设置内容视图。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Setup content view
setContentView(R.layout.content);
}
Display of indeterminate progress indicator by calling in onActivityCreated
通过调用onActivityCreated
显示不确定的进度指示器
setContentShown(false);
When the data is available, jut make sure to display the associated View
in the right place. 当数据可用时,请确保确保在正确的位置显示关联的View
。
if (this.isAdded()) setContentShown(true);
One thing you need to remember while implementing this library that you need to have a parent layout for the View
you want to show/hide which layout id will be android:id="@+id/content_container"
实现该库时需要记住的一件事是,您需要为要显示/隐藏的View
设置父布局,该布局ID将为android:id="@+id/content_container"
As I said earlier, there are some other ways too. 正如我之前所说,还有其他一些方法。 If you go through the Fragment
lifecycle , you'll definitely find a way to populate your data when the View
is available. 如果您经历了Fragment
生命周期 ,那么当View
可用时,您肯定会找到一种填充数据的方法。 Just populating your data inside onActivityCreated
should help you in this case too. 在这种情况下,仅将数据填充到onActivityCreated
也可以为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.