2017-08-08 2 views
-3

Je souhaite connaître le code permettant d'ajouter une barre de progression avant le chargement des images dans RecyclerView. Voici mon code RecyclerViewAdapter.Barre de progression lors du chargement d'images dans RecyclerView

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> 

ajouter déjà la barre de progression, mais il ne fonctionne pas, il y a juste un espace vide avant que les images sont chargées. J'ai supprimé le code d'importation ci-dessus pour minimiser le code affiché ici. Merci, désolé pour mon mauvais anglais.

+0

Pouvez-vous montrer les codes pour votre mise en page 'laundry_card'? –

+0

J'ai modifié le post ci-dessus –

+0

N'utilisez pas ProgressBar. Glide devrait avoir une méthode pour définir un drawable au moment du chargement - espace réservé, vous pouvez l'utiliser. – Selvin

Répondre

0

Vous pouvez essayer mes codes, besoin de mettre Visible pour barre de progression avant de charger l'image par Glide pour voir votre barre de progression:

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) 
; 
+0

J'ai une erreur dans progressBar.setVisibility (View.GONE), j'ai essayé de le changer à holder.progress.setVisibility (View.GONE) mais il a dit que le détenteur doit être déclaré comme final. Ensuite, j'ai essayé de l'appliquer, puis une nouvelle erreur dans RequestListener –