页面仍在加载时,进度对话框将消失,然后出现在屏幕上

时间:2019-06-07 10:04:32

标签: android firebase android-recyclerview progressdialog firebaseui

我有一个firebase recyclerview,我试图在其中填充firebase数据库中的一些数据。初始页面加载2秒钟后,数据加载非常平稳。最初加载页面时,通常会显示2或3秒的黑屏。在Firebase Recycler视图完成从Firebase数据库中检索数据之后,我尝试在此期间放置一个进度对话框,并关闭了进度对话框。但是进度对话框似乎从来没有出现过。我将在Firebase recyclerview加载完成后立即在onCreate()方法中初始化进度对话框,并在onResume()方法中取消进度对话框

我尝试使用进度条代替进度对话框,但是问题仍然与我提到的相同。我还尝试使用android Handler和timer方法为其提供默认计时,但这将是静态方法,不足以解决长期问题

    private ProgressDialog loadingBar;

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


    // loading bar
    loadingBar = new ProgressDialog(this);
    loadingBar.setMessage("Please wait...");
    loadingBar.setCanceledOnTouchOutside(false);
    loadingBar.show();

     //Recyclerview and linear layout manager
    search_task_list_recycler = 
    findViewById(R.id.search_task_list_recycler);
    linearLayoutManager = new LinearLayoutManager(this);
    search_task_list_recycler.setHasFixedSize(true);
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    search_task_list_recycler.setLayoutManager(linearLayoutManager);


     }


 //   ====================================================================

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

    // displaytask() contains the firebase recyclerview retrieval method 
    and recyclerview view 

    displayTasks();

    loadingBar.dismiss();


}


 //   ====================================================================

       private void displayTasks() {



       Query query = search_task_list;

       FirebaseRecyclerOptions<Search_task_details> options = new 
       FirebaseRecyclerOptions.Builder<Search_task_details>()
            .setQuery(query, Search_task_details.class).build();
       final FirebaseRecyclerAdapter<Search_task_details, ViewHolder> 
       adapter =
            new FirebaseRecyclerAdapter<Search_task_details, ViewHolder> 
        (options) {
                @Override
                protected void onBindViewHolder(@NonNull final ViewHolder 
      holder, int position, @NonNull final Search_task_details model) {



      UsersRef.child(model.getPoster_id()).addValueEventListener(new 
      ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot 
       dataSnapshot) {
                            if (dataSnapshot.exists()) {
                                if 
      (dataSnapshot.hasChild("profile_image")) {



Picasso.get().load(dataSnapshot.child("profile_image").getValue().toString())
                                             .placeholder(R.drawable.submit_profile_image)
                                            .into(holder.search_task_user_image);
                                }
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });


                    holder.search_task_topic_title.setText(model.getTopic_title());
                    holder.search_task_due_date.setText(model.getDue_date());
                    holder.search_task_preferred_timing.setText(model.getPreferred_time());
                    holder.search_task_location.setText(model.getTask_location());
                    holder.search_task_job_status.setText(model.getJob_status());
                    holder.search_task_budget.setText(model.getSum_total() + " B.D");


                    String givenDateString = model.getCurrent_calendar_Date_and_Time();
                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy, HH:mm", Locale.ENGLISH);
                    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Bahrain"));

                    long time_in_milliseconds = 0;

                    Date date = new Date();
                    try {
                        date = sdf.parse(givenDateString);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    time_in_milliseconds = date.getTime();


                    final CharSequence ch = DateUtils.getRelativeTimeSpanString(time_in_milliseconds
                            , System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS);
                    holder.search_task_time_ago.setText(ch);



                    holder.search_task_linear_layout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            postKey = getRef(holder.getAdapterPosition()).getKey();
                            Bundle bundle = new Bundle();
                            Intent goToTaskDetails = new Intent(Search_task_list.this, task_details.class);
                            bundle.putString("post_key", postKey);
                            bundle.putString("poster_id", model.getPoster_id());
                            goToTaskDetails.putExtras(bundle);
                            startActivity(goToTaskDetails);
                        }
                    });


                }

                @NonNull
                @Override
                public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.search_task_design_details, parent, false);
                    ViewHolder viewHolder = new ViewHolder(view);
                    return viewHolder;
                }
            };


     search_task_list_recycler.setAdapter(adapter);
     adapter.startListening();



}


//================================================================

    public static class ViewHolder extends RecyclerView.ViewHolder {
    public CircleImageView search_task_user_image;
    public AppCompatTextView search_task_topic_title;
    public AppCompatTextView search_task_due_date;
    public AppCompatTextView search_task_preferred_timing;
    public AppCompatTextView search_task_location;
    public AppCompatTextView search_task_time_ago;
    public AppCompatTextView search_task_job_status;
    public AppCompatTextView search_task_budget;
    public LinearLayout search_task_linear_layout;


    public ViewHolder(View itemView) {
        super(itemView);

        search_task_user_image = itemView.findViewById(R.id.search_task_user_image);
        search_task_topic_title = itemView.findViewById(R.id.search_task_topic_title);
        search_task_due_date = itemView.findViewById(R.id.search_task_due_date);
        search_task_preferred_timing = itemView.findViewById(R.id.search_task_preferred_timing);
        search_task_location = itemView.findViewById(R.id.search_task_location);
        search_task_time_ago = itemView.findViewById(R.id.search_task_time_ago);
        search_task_job_status = itemView.findViewById(R.id.search_task_job_status);
        search_task_budget = itemView.findViewById(R.id.search_task_budget);
        search_task_linear_layout = itemView.findViewById(R.id.search_task_linear_layout);


    }
}

需要结果

只要从firebase数据库中获取了firebase base recyclerview数据,进度对话框就应该显示。取决于其他因素(例如Internetspeed或其他因素),它可能是1秒或2秒或更长时间。

1 个答案:

答案 0 :(得分:0)

  

请尝试

private ProgressDialog loadingBar;

onCreate(Bundle savedInstanceState) {
     // loading bar
     loadingBar = new ProgressDialog(this);
     loadingBar.setMessage("Please wait...");
     loadingBar.setCanceledOnTouchOutside(false);
     loadingBar.show();

     displayTasks();
}
  

在您的displayTasks()

private void displayTasks() 
{
    search_task_list_recycler.setAdapter(adapter);
    adapter.startListening();

    //here add
    loadingBar.dismiss();
 }

希望这会对您有所帮助。