【问题标题】:How can I add progressbar before the load data from firebase and dismiss this progressbar after loaded data from firebase with firebasercycler adapter如何在从 firebase 加载数据之前添加进度条,并在使用 firebasercycler 适配器从 firebase 加载数据后关闭此进度条
【发布时间】:2019-03-25 13:48:46
【问题描述】:

我创建了一个应用程序,在这个应用程序中我使用了 firebase 和 firebaserecycler 适配器。我可以加载数据,它工作正常。但是我想在加载数据之前添加进度条,并在加载数据后将其关闭。有人有这方面的经验吗?

我的片段是这个 //

    public class CategoryFragment extends Fragment {
    View view;

    RecyclerView listCategory;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;

    FirebaseDatabase database;
    DatabaseReference categories;

    public static CategoryFragment newInstance(){
    CategoryFragment categoryfragment = new CategoryFragment();
    return categoryfragment;
    }

    public CategoryFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    database = FirebaseDatabase.getInstance();
    categories = database.getReference("Category");

    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup 
    container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_category, container, 
    false);

    listCategory = view.findViewById(R.id.listCategory);
    listCategory.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(container.getContext());
    listCategory.setLayoutManager(layoutManager);

    loadCategories();

    return view;
    }

    private void loadCategories(){
    adapter =  new FirebaseRecyclerAdapter<Category, CategoryViewHolder>
       (
            Category.class,
            R.layout.category_layout,
            CategoryViewHolder.class,
            categories
       ) {
        @Override
        protected void populateViewHolder(CategoryViewHolder viewHolder, 
        final Category model, int position) {
            viewHolder.category_name.setText(model.getName());

            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean 
         isLongClick) {
                    Intent game = new Intent(getActivity(),Start.class);
                    Common.categoryId = 
         adapter.getRef(position).getKey();
                    startActivity(game);
                }
            });
        }
       };
       adapter.notifyDataSetChanged();
       listCategory.setAdapter(adapter);
       }
       }

我的 xml 文件是这个 //

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".CategoryFragment">

  <android.support.v7.widget.RecyclerView
    android:id="@+id/listCategory"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </android.support.v7.widget.RecyclerView>

  </FrameLayout>

【问题讨论】:

    标签: android firebase-realtime-database android-recyclerview firebaseui


    【解决方案1】:

    因为您使用的是FirebaseRecyclerAdapter,所以可以很简单地解决这个问题。创建ProgressBar 的新对象并开始在onCreate() 方法中显示它,或者如果需要,直接将其添加到您的.XML 文件中。最后在您的适配器类中,覆盖以下方法:

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

    附:您正在使用一个非常旧版本的 Firebase-UI 库。请更新到最新版本。

    【讨论】:

    • 这是完整的代码。您应该在 FirebaseRecyclerAdapter 类中实现此方法。应该是在populateViewHolder()等其他方法附近的地方吧?
    猜你喜欢
    • 2019-11-13
    • 2018-12-25
    • 1970-01-01
    • 2019-02-03
    • 2018-08-16
    • 2011-06-21
    • 1970-01-01
    • 2011-12-27
    • 2016-12-10
    相关资源
    最近更新 更多