Progressler,而RecyclerView在firebase中搜索值

时间:2018-04-05 01:24:51

标签: android firebase android-recyclerview android-progressbar

晚安。 通常,RecyclerView会延迟返回Firebase值,这会导致我的主窗口此时变为空白。 我想放一个ProgressBar,而Recyclerview什么都不返回。 我想要一些简单的东西,在登录屏幕上我能够做到这一点,同时验证用户和密码是否有效。但在我的主屏幕上我不能,我尝试了一切,我研究了一切。 有人能帮我吗?谢谢。

这是我的代码:

 import android.content.Context;
 import android.content.Intent;
 import android.support.annotation.NonNull;
 import android.support.design.widget.FloatingActionButton;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.firebase.ui.database.FirebaseRecyclerAdapter;
 import com.google.android.gms.tasks.OnCompleteListener;
 import com.google.android.gms.tasks.Task;
 import com.google.firebase.auth.AuthResult;
 import com.google.firebase.auth.FirebaseAuth;
 import com.google.firebase.database.DatabaseReference;
 import com.google.firebase.database.FirebaseDatabase;
 import com.squareup.picasso.Picasso;

 public class MainActivity extends AppCompatActivity {
 FloatingActionButton contatoLojas;
 private RecyclerView mBlogList;
 FirebaseDatabase database;
 DatabaseReference myRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contatoLojas = (FloatingActionButton)findViewById(R.id.contatolojas);
    contatoLojas.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, ContatoLojasActivity.class));
        }
    });

    //Recycler View
    mBlogList = (RecyclerView)findViewById(R.id.blog_list);
    mBlogList.setHasFixedSize(true);
    mBlogList.setLayoutManager(new LinearLayoutManager(this));

    //Send a Query to the database
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("Data");
}


@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<ModelClass, BlogViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<ModelClass, BlogViewHolder>(
                    ModelClass.class,
                    R.layout.design_row,
                    BlogViewHolder.class,
                    myRef){

                @Override
                protected void populateViewHolder(BlogViewHolder viewHolder, ModelClass model, int position) {
                    viewHolder.setTitle(model.getTitle());
                    viewHolder.setValor(model.getValor());
                    viewHolder.setDesc(model.getDesc());
                    viewHolder.setImage(getApplicationContext(), model.getImage());
                }
            };
   mBlogList.setAdapter(firebaseRecyclerAdapter);
}

public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder (View itemView){
    super(itemView);
    mView= itemView;
}

public void setTitle (String title){
    TextView post_title = (TextView)mView.findViewById(R.id.titleText);
    post_title.setText(title);
}

public void setValor (String valor){
    TextView post_title2 = (TextView)mView.findViewById(R.id.titleText2);
    post_title2.setText(valor);
}

public void setDesc (String desc){
    TextView post_title3 = (TextView)mView.findViewById(R.id.titleText3);
    post_title3.setText(desc);
}

public void setImage (Context ctx , String image){
    ImageView post_image = (ImageView)mView.findViewById(R.id.imageViewy);
    // We need TO pass Context
    Picasso.with(ctx).load(image).into(post_image);
   // Picasso.with(ctx).load(image).into(post_image);
}
 }}

这是登录屏幕代码,工作正常。

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class SplashScreen extends AppCompatActivity {

FirebaseDatabase database1;
DatabaseReference myRef1;
private EditText edtLogin;
private EditText edtSenha;
private Button btnVerOfertas;
private ProgressBar progressCarregando;
private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

    edtLogin = (EditText) findViewById(R.id.edt_login);
    edtSenha = (EditText) findViewById(R.id.edt_senha);
    btnVerOfertas = (Button) findViewById(R.id.btn_VerOfertas);
    progressCarregando = (ProgressBar) findViewById(R.id.progress_Carregando);

    mAuth = FirebaseAuth.getInstance();

    String email = edtLogin.getText().toString();
    String password = edtSenha.getText().toString();

    if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
        progressCarregando.setVisibility(View.VISIBLE);

        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    sendToMain();
                    progressCarregando.setVisibility(View.INVISIBLE);
                }else {
                    Toast.makeText(SplashScreen.this, "Erro : " + task.getException().getMessage(), Toast.LENGTH_LONG).show();
                    progressCarregando.setVisibility(View.INVISIBLE);
                }
            }
        });
    }
}

}

2 个答案:

答案 0 :(得分:1)

要解决此问题,请在.XML布局文件中添加ProgressBar视图,如下所示:

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progress_bar"
    android:layout_gravity="center"/>

然后在您的活动类中找到它:

progressBar = (ProgressBar) findViewById(R.id.progress_bar);

不要忘记将其声明为全局变量:

private ProgressBar progressBar;

然后在FirebaseRecyclerAdapter班级内,只需覆盖以下方法:

@Override
public void onDataChanged() {
    if (progressBar != null) {
        progressBar.setVisibility(View.GONE);
    }
}

这意味着当活动开始时,进度条将处于活动状态且可见,当您从数据库中获取数据时,进度条将消失。

答案 1 :(得分:0)

您可以使用以下代码显示进度条而不是空白屏幕: -

首先,创建一个方法,如 showLoader()

protected Dialog loadDialog = null;  // make it global for single activity - in your case Main window.

    protected void showLoader(Context context) {
        if (loadDialog != null) {
            if (loadDialog.isShowing()) {
                if (!((Activity) context).isFinishing()) {
                    loadDialog.dismiss();
                }
            }
        }
        loadDialog = new Dialog(context, R.style.TransparentDialogTheme);
        loadDialog.setContentView(R.layout.spinner_rotate);
        loadDialog.setCanceledOnTouchOutside(false);

        loadDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });


        if (loadDialog != null && !((Activity) context).isFinishing() && !loadDialog.isShowing()) {
            loadDialog.show();
        }
    }

将以下依赖添加到gradle

 compile 'com.wang.avi:library:2.1.3'

spinner_rotate.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:background="@android:color/transparent"
     android:gravity="center">

    <com.wang.avi.AVLoadingIndicatorView
    style="@style/AVLoadingIndicatorView.Large"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:visibility="visible"
     app:indicatorColor="@color/colorBlue"
     app:indicatorName="BallClipRotateIndicator" />

</LinearLayout>

之前(在上面的行)编写从firebase获取数据设置数据的代码时,您可以调用此方法适配器 当您在适配器中设置数据时,您需要使用下面的hideLoader()方法隐藏该加载器。

void hideSpinner() {
        if (loadDialog.isShowing()) {
            if (!(BaseActivity.this).isFinishing()) {
                loadDialog.dismiss();
            }

        }

    }

显示可能对您有帮助的整个代码

 showSpinner(this);

        BaseActivity.mDatabase.child("your database's child name where you want to get data").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot != null && dataSnapshot.hasChildren()) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {

//          here add data to list which are fetched from firebase

                }
            }

            if (adapter == null) {
//                     Then set Your adapter here ..
            }

            hideSpinner();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            hideSpinner();

        }
    });

我希望它可以帮助你。