我如何才能在从Firebase加载数据之前添加进度条,并在使用firebasercycler适配器从firebase加载数据后关闭此进度条

时间:2019-03-25 13:48:46

标签: android firebase-realtime-database recycler-adapter firebaseui

我创建了一个应用程序,在该应用程序中,我使用了firebase和firebaserecycler适配器。我可以加载数据,它工作正常。但是我想要添加进度条直到数据被加载,并在数据加载后将其关闭。有人对此有经验吗?

我的片段是这个//

if find_num_3 not in {None, i, j}:
    #...

我的xml文件是这个//

    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);
       }
       }

1 个答案:

答案 0 :(得分:2)

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

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

P.S。您正在使用Firebase-UI库的非常旧的版本。请更新到最新版本。