Android Studio – 让一个活动等待直到UI加载完成

huangapple 未分类评论57阅读模式
标题翻译

Android Studio - Make an activity wait until loading UI

问题

我有一个包含`recyclerView`的活动,该`recyclerView`从数据库加载数据。有时在启动活动时,直到重新加载活动后才看到数据。

我尝试做的是显示一个加载动画持续 3 秒钟,等待数据加载完成,然后再显示数据。

我将以下内容添加到我的 XML 中:

    <RelativeLayout
        android:id="@+id/loadingPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true" />
    </RelativeLayout>

但是如何使我的活动等待 3 秒钟(显示加载动画),然后返回到已加载数据的活动?
英文翻译

I have an activity which contains a recyclerView that loads data from the database. Sometimes when starting the activity I don't see the data until reloading the activity.

What I'm trying to do is show a loading animation for 3 seconds and wait for it to load then show it.

I added this to my xml:

&lt;RelativeLayout
    android:id=&quot;@+id/loadingPanel&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:gravity=&quot;center&quot; &gt;

    &lt;ProgressBar
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:indeterminate=&quot;true&quot; /&gt;
&lt;/RelativeLayout&gt;

But how can I make my activity wait for 3 seconds (showing the loading animation) and then go back to the activity with loaded data?

答案1

得分: 0

  1. 创建一个回调类:

    interface Callback {
        public void onStart();
        public void onComplete(<your_data_type> data);
    }
    
  2. 将回调作为参数添加到你的方法中,该方法从数据库加载数据,然后在事件发生时需要调用回调的方法:

    public void loadData(Callback callback) {
        callback.onStart();
        <your_data_type> data = /* 从数据库加载数据 */;
        callback.onComplete(data);
    }
    
  3. 将一个实现了回调的对象传递给loadData方法:

    loadData(new Callback {
        public void onStart() {
            // 开始显示进度条
        }
        public void onComplete(<your_data_type> data) {
            // 在你的可循环视图中显示数据
            // 隐藏进度条
        }
    });
    

当然,你需要在后台线程中调用loadData()方法,并在主线程中调用回调的方法。

英文翻译
  1. Create a callback class:

    interface Callback {
    public void onStart();
    public void onComplete(&lt;your_data_type&gt; data);
    }

  2. Add a callback as an argument to your method, that loads data from database, then you need to call the callback's methods when the events happen:

    public void loadData(Callback callback) {
    callback.onStart();
    &lt;your_data_type&gt; data = /* load data from database */;
    callback.onComplete(data);
    }

  3. Pass it an implemented callback to loadData method:

    loadData(new Callback {
    public void onStart() {
    // start showing your progress bar
    }
    public void onComplete(&lt;your_data_type&gt; data) {
    // show data in your recycler view
    // hide your progress bar
    }
    });

And of course you need to call loadData() method in a background thread and call the callback's methods in a main thread.

huangapple
  • 本文由 发表于 2020年1月30日 19:03:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/59984524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定