当我的数据从firebasedatabase检索到recyclerview时,我想显示一个Progressbar,并在完成后隐藏它。我在网上搜索,但没有解决方案。工作
这是我的主要活动
公开课MemesSchool扩展AppCompatActivity{
RecyclerView recyclerView;
MemeAdapter memeAdapter;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressBar = (ProgressBar) findViewById(R.id.pbmeme);
setContentView(R.layout.activity_memes_school);
getWindow().setStatusBarColor(getResources().getColor(R.color.buttonclr));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.buttonclr)));
getSupportActionBar().setTitle("School Memes");
recyclerView = findViewById(R.id.rvm);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions < mememodel > options =
new FirebaseRecyclerOptions.Builder < mememodel > ()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Memedata"), mememodel.class)
.build();
memeAdapter = new MemeAdapter(options);
recyclerView.setAdapter(memeAdapter);
}
@Override
protected void onStart() {
super.onStart();
memeAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
memeAdapter.stopListening();
}
}
这是我的MainActivity.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"
tools:context=".MemesSchool">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/rvm"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foregroundGravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/pbmeme"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的Adapterclass
public class MemeAdapter extends FirebaseRecyclerAdapter < mememodel, MemeAdapter.myViewHolder > {
public MemeAdapter(@NonNull FirebaseRecyclerOptions < mememodel > options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull mememodel model) {
holder.username.setText(model.getNameuser());
holder.subuser.setText(model.getSubname());
holder.title.setText(model.getTitle());
Glide.with(holder.imgmeme.getContext()).load(model.getImagesurl())
.placeholder(R.drawable.logoinhanced)
.error(R.drawable.logoinhanced).into(holder.imgmeme);
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.memes_item, parent, false);
return new myViewHolder(view);
}
@Override
public void onDataChanged() {
}
@Override
public void onError(@NonNull DatabaseError error) {
super.onError(error);
}
class myViewHolder extends RecyclerView.ViewHolder {
ImageView imgmeme;
TextView username, subuser, title;
public myViewHolder(@NonNull View itemView) {
super(itemView);
imgmeme = itemView.findViewById(R.id.meme);
username = itemView.findViewById(R.id.nameuser);
subuser = itemView.findViewById(R.id.subname);
title = itemView.findViewById(R.id.titlememe);
}
}
}
当应该在适配器类中时,有一个名为ONDATACHANGE((的Mothod,我不知道如何从那里控制我的适配器类;
应该发生什么:-当我打开活动时,Progressbar应该是可见的,当数据加载到RecyclerView中时,它应该隐藏!
加载数据时隐藏进度条的一种方法是将其传递给其构造函数中的适配器,然后将其隐藏在onDataChanged
:中
public class MemeAdapter extends FirebaseRecyclerAdapter < mememodel, MemeAdapter.myViewHolder > {
View progressBar;
public MemeAdapter(@NonNull FirebaseRecyclerOptions <mememodel> options, View progressBar) {
super(options);
this.progressBar = progressBar;
}
...
@Override
public void onDataChanged() {
progressBar.setVisibility(View.GONE)
}