英文:
how to Progress bar show before image load.?
问题
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
// ... (other code)
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
// ... (other code)
try {
// Show progress bar before loading image
holder.progressBar.setVisibility(View.VISIBLE); // Show progress bar
Glide.with(mContext.getApplicationContext())
.load(post.getPostimage())
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
// Handle image load failure
holder.progressBar.setVisibility(View.GONE); // Hide progress bar
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
// Image loaded successfully
holder.progressBar.setVisibility(View.GONE); // Hide progress bar
return false;
}
})
.into(holder.post_image);
} catch (Exception e) {
// Handle any exceptions
}
// ... (other code)
}
// ... (other code)
}
请注意,我已经在 onBindViewHolder
方法中添加了Glide的监听器,以便在图片加载过程中显示和隐藏进度条。在加载成功或失败时,它会相应地设置进度条的可见性。以上是你提供的代码的翻译部分,如果还有其他问题,请随时提问。
英文:
I want to show a progress bar before image loading. I am getting an image from firebase storage. some time image taking more time to load and show .. ... That's why I am trying to input progressbar before load image..... I have tried but now working. Can anyone help me to solve this issue????
Please check my code below:-
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
public Context mContext;
private List <Post>mPost;
private FirebaseUser firebaseUser;
ProgressBar progressBar;
public PostAdapter(Context mContext, List<Post> mPost) {
this.mContext = mContext;
this.mPost = mPost;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mContext).inflate(R.layout.post_item,parent,false);
return new PostAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
final Post post=mPost.get(position);
try {
Glide.with(mContext.getApplicationContext()).load(post.getPostimage()).into(holder.post_image);
}catch (Exception e)
{
}
//holder.time.setText(post.getTime());
Get_Time_ago getTimeAgo=new Get_Time_ago();
long lastTime=Long.parseLong(String.valueOf(post.timestamp));
String last_seen_time=getTimeAgo.getTimeAgo(lastTime,mContext);
holder. time.setText(last_seen_time);
/*SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String time=sfd.format(new Date(lastseenonline));
mLast_seenview.setText(time);*/
if(post.getDescription().equals("")){
holder.description.setVisibility(View.GONE);
}else {
holder.description.setVisibility(View.VISIBLE);
holder.description.setText(post.getDescription());
}
publisherInfo(holder.image_profile,holder.username,holder.blood,holder.publisher,post.getPublisher());
if(post.getPublisher().equals(firebaseUser.getUid())){
holder.delete.setVisibility(View.VISIBLE);
}else {
holder.delete.setVisibility(View.GONE);
}
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("Confirm Delete?");
alertDialog.setMessage("Do you want to delete your post?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
FirebaseDatabase.getInstance().getReference("posts").child(post.getPostid()).removeValue();
notify();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
}
@Override
public int getItemCount() {
return mPost.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView image_profile,post_image,like,dislike,comment,save,delete;
public TextView username,blood,likes,dislikes,comments, description,publisher,time;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image_profile=itemView.findViewById(R.id.image_profile);
post_image=itemView.findViewById(R.id.post_image);
like=itemView.findViewById(R.id.like);
comment=itemView.findViewById(R.id.comment);
save=itemView.findViewById(R.id.save);
delete=itemView.findViewById(R.id.delete);
username=itemView.findViewById(R.id.username);
blood=itemView.findViewById(R.id.blood);
time=itemView.findViewById(R.id.time);
likes=itemView.findViewById(R.id.likes);
dislikes=itemView.findViewById(R.id.dislikes);
dislike=itemView.findViewById(R.id.dislike);
comments=itemView.findViewById(R.id.comments);
description=itemView.findViewById(R.id.description);
publisher=itemView.findViewById(R.id.publisher);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
}
}
private void publisherInfo (final ImageView image_profile, final TextView username,final TextView blood, final TextView publisher, final String userId ){
DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Users").child(userId);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
User user=dataSnapshot.getValue(User.class);
Glide.with(mContext.getApplicationContext()).load(user.getImageurl()).into(image_profile);
username.setText(user.getUsername());
blood.setText(user.getBlood());
publisher.setText(user.getUsername());
}catch (Exception e){
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
答案1
得分: 1
你可以在Glide的占位符中使用CircularProgressDrawable来显示加载状态。我从我的小项目中复制了一段代码,它使用Kotlin编写,但很简单:
Kotlin代码:
fun AppCompatImageView.loadImage(imageUri: String) {
val circularProgressDrawable = CircularProgressDrawable(context)
circularProgressDrawable.strokeWidth = 5f
circularProgressDrawable.centerRadius = 30f
circularProgressDrawable.start()
Glide.with(context)
.load(imageUri)
.placeholder(circularProgressDrawable)
.error(R.drawable.ic_close_black)
.override(200, 300)
.into(this)
}
根据您的评论更新后的Java代码:
void loadImage(AppCompatImageView yourImageView, String imageUri) {
CircularProgressDrawable circularProgressDrawable = new CircularProgressDrawable(yourImageView.getContext());
circularProgressDrawable.setStrokeWidth(5f);
circularProgressDrawable.setCenterRadius(30f);
circularProgressDrawable.start();
Glide.with(yourImageView)
.load(imageUri)
.placeholder(circularProgressDrawable)
.error(R.drawable.ic_close_black)
.override(200, 300)
.into(yourImageView);
}
英文:
You can show a loading using a CircularProgressDrawable in the Glides´s placeholder. I copied it from my little project, it is in Kotlin, but is easy:
fun AppCompatImageView.loadImage(imageUri: String) {
val circularProgressDrawable = CircularProgressDrawable(context)
circularProgressDrawable.strokeWidth = 5f
circularProgressDrawable.centerRadius = 30f
circularProgressDrawable.start()
Glide.with(context)
.load(imageUri)
.placeholder(circularProgressDrawable)
.error(R.drawable.ic_close_black)
.override(200, 300)
.into(this)
}
Update after your comment... code in Java:
void loadImage(AppCompatImageView yourImageView, String imageUri) {
CircularProgressDrawable circularProgressDrawable = new CircularProgressDrawable(yourImageView.getContext())
circularProgressDrawable.setStrokeWidth(5f);
circularProgressDrawable.setCenterRadius(30f);
circularProgressDrawable.start();
Glide.with(yourImageView)
.load(imageUri)
.placeholder(circularProgressDrawable)
.error(R.drawable.ic_close_black)
.override(200, 300)
.into(yourImageView);
}
答案2
得分: 0
像这样在Java中:
private void loadImage(ImageView yourImageView, String imageUri) {
CircularProgressDrawable circularProgressDrawable = new CircularProgressDrawable(yourImageView.getContext());
circularProgressDrawable.setStrokeWidth(5f);
circularProgressDrawable.setCenterRadius(30f);
circularProgressDrawable.start();
Glide.with(yourImageView.getContext())
.load(imageUri)
.placeholder(circularProgressDrawable)
.error(R.drawable.ic_close_black)
.override(200, 300)
.into(yourImageView);
}
英文:
Like this in Java:
private void loadImage(ImageView yourImageView, String imageUri) {
CircularProgressDrawable circularProgressDrawable = new CircularProgressDrawable(yourImageView.getContext())
circularProgressDrawable.setStrokeWidth(5f)
circularProgressDrawable.setCenterRadius(30f)
circularProgressDrawable.start()
Glide.with(yourImageView.getContext())
.load(imageUri)
.placeholder(circularProgressDrawable)
.error(R.drawable.ic_close_black)
.override(200, 300)
.into(yourImageView);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论