【问题标题】:Display a loading icon until Firebase firestore fully loads data into a textview显示加载图标,直到 Firebase Firestore 将数据完全加载到文本视图中
【发布时间】:2020-04-22 16:01:11
【问题描述】:

当我的应用将 Firebase Firestore 数据加载到其中时,如何实现加载图标?现在,当我加载应用程序时,它显示一个空的文本视图,一旦它从数据库中获取数据,它就会弹出。我希望有一个加载屏幕,直到数据在显示之前完全加载到活动中,所以没有数据弹出。

我尝试查看此网站上的其他解决方案,但它们都来自不同的编程语言,或者我难以理解。

这是我从数据库中获取数据并将其存储在 textView 中的简单代码。

    welcomeTv = findViewById(R.id.welcomeTv);
    fStore = FirebaseFirestore.getInstance();

    fStore.collection("Users").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {

            if (task.isSuccessful() && task.getResult() != null) {
                username = task.getResult().getString("username");
                welcomeTv.setText(username);

                //other stuff
            } else {
                Toast.makeText(HomeActivity.this, "Currently logged in", Toast.LENGTH_SHORT).show();
            }


        }
    });

【问题讨论】:

    标签: java android firebase android-studio


    【解决方案1】:

    在您的项目中添加SweetAlert。因此,当您的数据正在加载时,您可以输入您的甜蜜警报。

    使用以下库:

    implementation 'com.github.f0ris.sweetalert:library:1.5.1'
    

    当您触发加载数据的事件时启动sweetalert,然后当接收到数据时它将关闭

    //Global Declaration
    SweetAlertDialog pDialog;
    
    pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
    pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
    pDialog.setTitleText("Loading");
    pDialog.setCancelable(false);
    pDialog.show();
    fStore.collection("Users").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
    
                if (task.isSuccessful() && task.getResult() != null) {
                    pDialog.dismiss();
                    pDialog.dismissWithAnimation();
                    username = task.getResult().getString("username");
                    welcomeTv.setText(username);
    
                    //other stuff
                } else {
                    pDialog.dismiss();
                    pDialog.dismisswithAnimation();
                    Toast.makeText(HomeActivity.this, "Currently logged in", Toast.LENGTH_SHORT).show();
                }
    
    
            }
    });
    

    【讨论】:

    • 使其成为全局变量。
    • 谢谢,我已经成功了,对您的解决方案稍作修正:dismisswithAnimation 应该是dismissWithAnimation。通过这种加载,您仍然可以在后台看到数据弹出,有没有办法使它成为一个完整的页面加载,然后一旦加载,页面就会加载。因为现在,您看到页面很短的时间,然后加载对话框打开,然后在数据加载后关闭。
    • 你能提供完整的代码然后我提供完整的正确代码
    • 所以你想要在 mAuth 之前和之后的进度条?
    • 我认为 Auth 是该页面不可或缺的一部分,因为它仅在用户登录时才有效,我不太确定在 mAuth 之前或之后是否会有所不同?
    【解决方案2】:

    加载数据后,您可以使用ProgressBar 和调用ProgressBar.setVisibility(View.GONE) 隐藏进度条。

    首先在布局的其余部分之外添加ProgressBar,然后将要隐藏的布局的可见性设置为已消失

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/mainContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"> // Initially hide all the body
    
            <TextView
                android:id="@+id/welcomeTv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Welcome"
                app:layout_constraintBottom_toTopOf="@+id/logOutBtn"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.84000003" />
    
            <Button
                android:id="@+id/logOutBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Log out"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    一旦加载数据,隐藏 ProgressBar 并使 mainContent 布局可见

    ProgressBar progressBar = findViewById(R.id.progressBar); // Get ProgressBar reference
    ConstraintLayout mainContent = findViewById(R.id.mainContent);
    welcomeTv = findViewById(R.id.welcomeTv);
    fStore = FirebaseFirestore.getInstance();
    
    
    fStore.collection("Users").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                progressBar.setVisibility(View.GONE); // Hide Progress bar 
                mainContent.setVisibility(View.VISIBLE); // Show TextView
    
                if (task.isSuccessful() && task.getResult() != null) {
    
                    username = task.getResult().getString("username");
                    welcomeTv.setText(username);
    
                    //other stuff
                } else {
                    Toast.makeText(HomeActivity.this, "Currently logged in", Toast.LENGTH_SHORT).show();
                }
    
    
            }
        });
    

    希望对你有帮助

    更新:用您在 cmets 中提到的布局更新了代码。

    【讨论】:

    • 感谢您的解决方案,RelativeLayout 不适用于我的解决方案,所以我使用 ConstraintLayout,有没有办法让活动中的所有内容在 textview 加载数据之前不可见?现在进度条位于注销按钮的后面,但希望用户在进度条加载数据之前看不到任何内容。谢谢。
    • 当然可以。您可以使用与 TextView 相同的方式进行操作。只需将您想要隐藏的所有内容包装在 ConstraintLayout 中并给它一个id 并将可见性设置为消失。然后使用 id 获取对布局的引用,并在加载数据后在此布局上调用 setVisibilty(View.VISIBLE)
    • @AadilFarooq 用您的布局代码更新了答案。试着告诉我。
    • 非常感谢,这正是我想要的,非常感谢您的帮助!我确实有一个关于启动画面的附带问题,如果我能和你讨论一下,我会欠你的! :)
    • 所以我已经实现了一个启动屏幕,它是主屏幕和启动器,但是当启动屏幕出现时,它会在短时间内被推入通知栏并返回。我认为这与此人遇到的问题类似:github.com/crazycodeboy/react-native-splash-screen/issues/385 将 android:windowBackground 更改为 android:background 不幸的是,对此无济于事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    相关资源
    最近更新 更多