简体   繁体   English

等到加载视图后,再将加载的数据放入其中

[英]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.

相关问题 如何等到从服务器加载数据 - how to wait until data from server is loaded 如何等待 Preferences DataStore 中的数据加载完毕? - How do I wait until data from Preferences DataStore is loaded? Android等待图像加载到ImageView中 - Android Wait Until An Image Has Loaded To the ImageView Android Webview:等待页面绘制,直到完全加载 - Android webview: wait with drawing of page until it's completely loaded 等待浏览器加载页面,清除缓存 - Wait Until Browser Has Loaded Page, Clear Cache 等待加载AdMob广告-有时广告不展示 - Wait until AdMob Ads are loaded - sometimes ads not showing ListView:如何等到它加载完毕? - ListView: how do I wait until it's loaded up? 在将数据加载到Recycler视图之前加载视图 - Loading view before Data is loaded into Recycler view 加载我的View时,我的ViewModel具有空值,如何延迟View的加载,直到ViewModel填充数据? - My ViewModel has null values when my View is loaded, how to delay loading of the View until the ViewModel populates the data? 一些数据稍后会在应用程序中加载,并且我的主页上会出现一个空的设计图像,直到它们被加载 android/java - some data is loaded later in the application and an empty design image appears on my homepage until they are loaded android/java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM