我想知道在我的RecyclerView中加载图像之前添加进度条的代码。这是我的RecyclerViewAdapter代码。
RecyclerViewAdapter.java
package com.example.rizkafs.laundrize;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<DataLaundry> rvData;
private Context context;
public RecyclerViewAdapter(List<DataLaundry> rvData) {
this.rvData = rvData;
}
public class ViewHolder extends RecyclerView.ViewHolder {
// di tutorial ini kita hanya menggunakan data String untuk tiap item
CardView cv;
public TextView nama;
public TextView alamat;
public TextView jenis;
public ImageView imageVIew;
public ProgressBar progress;
public ViewHolder(View v) {
super(v);
cv = (CardView) v.findViewById(R.id.card_view);
nama = (TextView) v.findViewById(R.id.txt_judul);
alamat = (TextView) v.findViewById(R.id.alamat);
jenis = (TextView) v.findViewById(R.id.jenis);
imageVIew = (ImageView) v.findViewById(R.id.imageView);
progress = (ProgressBar) v.findViewById(R.id.progress);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// membuat view baru
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.laundry_card, parent, false);
// mengeset ukuran view, margin, padding, dan parameter layout lainnya
context = parent.getContext();
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int i) {
try {
// - mengambil elemen dari dataset (ArrayList) pada posisi tertentu
// - mengeset isi view dengan elemen dari dataset tersebut
DataLaundry list = rvData.get(i);
holder.nama.setText(list.getNama());
holder.alamat.setText(list.getAlamat());
holder.jenis.setText(list.getJenis());
Log.d("GetData", list.getHarga());
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://laundrize-68cee.appspot.com/");
StorageReference pathReference = storageRef.child(list.getImage_url().get(0));
Glide.with(context)
.using(new FirebaseImageLoader())
.load(pathReference)
.into(holder.imageVIew);
holder.progress.setVisibility(View.GONE);
holder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("dataLaundry", rvData.get(i));
context.startActivity(intent);
}
});
} catch (Exception e) {
}
}
}
laundry_card.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="11dp"
android:paddingLeft="10dp"
android:paddingRight="11dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:id="@+id/gambar">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/progress"/>
</RelativeLayout>
<TextView
android:id="@+id/txt_judul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/gambar"
android:textSize="17sp"
android:layout_marginTop="12dp"
android:textColor="#000000"
android:text="Judul"/>
<TextView
android:id="@+id/alamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="@id/txt_judul"
android:layout_marginTop="9dp"
android:layout_marginBottom="12dp"
android:layout_marginLeft="7dp"
android:textColor="#000000"
android:text="Alamat"/>
<TextView
android:id="@+id/jenis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:textSize="14sp"
android:layout_below="@id/alamat"
android:layout_marginTop="9dp"
android:layout_marginBottom="12dp"
android:text="Jenis"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我已经添加了进度条,但它不起作用,在加载图像之前只有一个空格。我删除了上面的导入代码,以最大限度地减少此处显示的代码。谢谢,抱歉我的英文不好。
答案 0 :(得分:0)
您可以尝试我的代码,在通过Glide加载图片之前需要为进度条设置Visible
以查看进度条:
progressBar.setVisibility(View.VISIBLE);
Glide.with(context)
.using(new FirebaseImageLoader())
.load(pathReference)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.into(holder.imageVIew)
;