如何在从 firebase 加载数据之前添加进度条,并在使用 firebasercycler 适配器从 firebase 加载数据后关闭此进度条
How can I add progressbar before the load data from firebase and dismiss this progressbar after loaded data from firebase with firebasercycler adapter
我创建了一个应用程序,在这个应用程序中我使用了 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 文件中。最后在您的适配器 class 中,覆盖以下方法:
@Override
public void onDataChanged() {
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
P.S。您使用的是非常旧版本的 Firebase-UI 库。请更新到最新版本。
我创建了一个应用程序,在这个应用程序中我使用了 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 文件中。最后在您的适配器 class 中,覆盖以下方法:
@Override
public void onDataChanged() {
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
P.S。您使用的是非常旧版本的 Firebase-UI 库。请更新到最新版本。