如何在Firebase recyclerview中加载时添加进度条

时间:2018-07-18 03:15:34

标签: android android-recyclerview android-progressbar

我正在构建一个应用,该应用使用Firebase Recycler适配器从Firebase提取数据,我需要在该Recyler视图中实现进度。

我无法获取如何一次加载特定数量的数据的方法,例如加载3个项目并显示进度栏,然后再加载其他项目。 并在开始加载项目时显示进度条

这是获取数据的代码

final ProgressBar p1=(ProgressBar)rootView.findViewById(R.id.StreetRecyclerProgress);
                final LinearLayout likeLayout=(LinearLayout)rootView.findViewById(R.id.Likee);
                MainArea.viewPager.setBackgroundResource(R.drawable.grey_background);
                myRef1 = FirebaseDatabase.getInstance().getReference().child("Video_Upload").child("Street");
                myRef1.keepSynced(true);
                myRef1.orderByKey();
                rView = (RecyclerView) rootView.findViewById(R.id.StreetRecyclerView);
                rView.hasFixedSize();
                LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()) {

                    @Override
                    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
                        LinearSmoothScroller smoothScroller = new LinearSmoothScroller(getActivity()) {

                            private static final float SPEED = 300f;// Change this value (default=25f)

                            @Override
                            protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                                return SPEED / displayMetrics.densityDpi;
                            }

                        };
                        smoothScroller.setTargetPosition(position);
                        startSmoothScroll(smoothScroller);
                    }

                };
                rView.setLayoutManager(layoutManager);
                FBRA = new FirebaseRecyclerAdapter<StreetDataFetcher, Street_food.StreetViewHolder>(
                        StreetDataFetcher.class,
                        R.layout.card_recycler_view,
                        Street_food.StreetViewHolder.class,
                        myRef1
                ) {
                    @Override
                    protected void populateViewHolder( Street_food.StreetViewHolder viewHolder, StreetDataFetcher model, final int position ) {
                        //p1.setVisibility(View.INVISIBLE);
                        viewHolder.setTitle(model.getVideoName());
                        viewHolder.setShortDesc(model.getShort_Desc());
                        viewHolder.setUsername(model.getUserName());
                        viewHolder.setDate(model.getDateTime());
                        viewHolder.setVideoPath(model.getVideoPath());
                        viewHolder.setProfile(model.getProfileImage());
                        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick( View v ) {
                                final String mref;
                                mref=FBRA.getRef(position).getKey().toString().trim();
                                //Toast.makeText(getActivity(),mref.toString(),Toast.LENGTH_SHORT).show();
                                 Intent i=new Intent(getActivity(),StreetVideoView.class);
                                i.putExtra("position",mref);
                                startActivity(i);

                            }
                        });
                        viewHolder.itemView.findViewById(R.id.Likee).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick( View v ) {
                                ImageView a;
                                a=(ImageView)v.findViewById(R.id.likeImage);
                                a.setImageResource(R.drawable.ic_heart_on);
                                final String mref;
                                mref=FBRA.getRef(position).getKey().toString().trim();
                                final DatabaseReference myyRef=FirebaseDatabase.getInstance().getReference().child("Video_Upload").child("Street").child(mref).child("Likes");
                                myyRef.addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange( DataSnapshot dataSnapshot ) {
                                        Integer i=Integer.valueOf(dataSnapshot.getValue().toString());
                                        int ii=i+1;
                                        String like_value= String.valueOf(ii);
                                        myyRef.setValue(like_value);
                                    }

                                    @Override
                                    public void onCancelled( DatabaseError databaseError ) {

                                    }
                                });
                            }
                        });
                    }
                };

                rView.setAdapter(FBRA);
                

此代码中我需要的是在回收者视图正在获取/加载数据时进度条应显示

2 个答案:

答案 0 :(得分:0)

将进度条可见性设置为VISIBLE和INVISIBLE可以解决问题。我在这样的片段中做到了

public void onDataChange(DataSnapshot dataSnapshot) {
        DiseaseFragment.show_progress();

                if (dataSnapshot.getChildrenCount() > 0) {
                    DiseaseFragment.Hide_no_content_message();
        DiseaseFragment.hide_progress();
                    // data is found
                }
                else{

                   /* If no data found do this */
                    new Handler().postDelayed(new Runnable() {
                                                  @Override
                                                  public void run() {
                                                      DiseaseFragment.hide_progress();

                                                  }
                                              },3000);}}

调用静态函数将进度条的可见性设置为可见/不可见。 其中3000是毫秒,等于3秒,如果找不到数据,请等待3秒并隐藏进度条

答案 1 :(得分:0)

在您的主要Fragment中,您可以使用静态方法,例如,

 public static void enableThem() {
             mProgressBar.setVisibility(View.VISIBLE);
             mTextNoPost.setVisibility(View.VISIBLE);
    }

    public static void disableThem() {
        mProgressBar.setVisibility(View.INVISIBLE);
        mTextNoPost.setVisibility(View.INVISIBLE);
    }

在回收适配器中,您可以覆盖onDataChanged()方法并添加实例化。

@Override
    public void onDataChanged() {
        super.onDataChanged();
        MainFragment.disableThem();
    }