我创建了一个应用程序,在这个应用程序中我使用了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>
因为您使用的是FirebaseRecyclerAdapter
,所以可以非常简单地解决这个问题。创建一个ProgressBar
的新对象并开始在onCreate()
方法中显示它,或者如果需要,可以直接在.XML文件中添加它。最后在适配器类中,重写以下方法:
@Override
public void onDataChanged() {
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
附:您使用的是非常旧版本的Firebase-UI库。请更新到最新版本。