【发布时间】:2021-02-08 20:10:39
【问题描述】:
所以我有可以从底部导航菜单访问的3 片段。当我选择“主页”时,它会打开一个 fragment,其中包含一个填充有来自 Firebase 数据库的数据的 RecyclerView。如何在recyclerview 从firebase 获取数据时显示progressbar?花了大约 1-2 秒,我想要一个 progressbar。
编辑:我创建了一个progressbar,但如果我在创建片段后将其可见性设置为 GONE,则 progressbar 根本不会出现。
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBarHome"
android:layout_width="100dp"
android:layout_height="39dp"
android:elevation="7dp"
android:visibility="visible"
android:indeterminateTint="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Java 文件:
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
carAdapter adapter; // Create Object of the Adapter class
DatabaseReference mbase;
private ProgressBar progressBar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container,false);
mbase = FirebaseDatabase.getInstance().getReference("Cars");
recyclerView = view.findViewById(R.id.recycler1);
// To display the Recycler view linearly
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// It is a class provide by the FirebaseUI to make a
// query in the database to fetch appropriate data
FirebaseRecyclerOptions<car> options = new FirebaseRecyclerOptions.Builder<car>()
.setQuery(mbase, car.class)
.build();
// Connecting object of required Adapter class to
// the Adapter class itself
adapter = new carAdapter(options);
// Connecting Adapter class with the Recycler view*/
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
progressBar = view.findViewById(R.id.progressBarHome);
progressBar.setVisibility(View.GONE);
}
@Override public void onStart()
{
super.onStart();
adapter.startListening();
}
// Function to tell the app to stop getting
// data from database on stoping of the activity
@Override public void onStop()
{
super.onStop();
adapter.stopListening();
}
}
【问题讨论】:
-
progressBar已经在布局中可见.. 你需要做的是在 RecyclerView 适配器接收数据时隐藏它.. 你能提供carAdapter代码吗? -
只需覆盖 onDataChanged 方法即可关闭 ProgressBar。
-
我的问题是如何在 carAdapter.java 中从布局“fragment_home.xml”访问 ID 为 progressBarHome 的进度条?我进行了一些研究,有人谈到了 Inflate,但我不知道我应该以哪种方法使用充气机或如何使它成为可能。提前致谢。