SHARE
TWEET

Untitled

a guest Jul 19th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.dekikurnia.appcelebrity.adapters;
  2.  
  3. import android.content.Context;
  4. import android.widget.ProgressBar;
  5. import androidx.annotation.NonNull;
  6. import androidx.recyclerview.widget.LinearLayoutManager;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;
  13. import butterknife.BindView;
  14. import butterknife.ButterKnife;
  15. import com.bumptech.glide.Glide;
  16. import com.bumptech.glide.request.RequestOptions;
  17. import com.dekikurnia.appcelebrity.R;
  18. import com.dekikurnia.appcelebrity.models.FilmPria;
  19. import com.dekikurnia.appcelebrity.ui.OnLoadMoreListener;
  20.  
  21. import java.util.List;
  22.  
  23. public class FilmPriaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  24.  
  25.     List<FilmPria> filmPriaList;
  26.     Context context;
  27.     private final int VIEW_TYPE_ITEM = 0;
  28.     private final int VIEW_TYPE_LOADING = 1;
  29.     private OnLoadMoreListener onLoadMoreListener;
  30.     private boolean isLoading;
  31.     private int visibleThreshold = 5;
  32.     private int lastVisibleItem, totalItemCount;
  33.  
  34.  
  35.     public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
  36.         this.onLoadMoreListener = mOnLoadMoreListener;
  37.     }
  38.  
  39.     @Override
  40.     public int getItemViewType(int position) {
  41.         return filmPriaList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
  42.     }
  43.  
  44.     public FilmPriaAdapter(RecyclerView recyclerView, List<FilmPria> filmPriaList, Context context) {
  45.         this.filmPriaList = filmPriaList;
  46.         this.context = context;
  47.  
  48.         final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
  49.         recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  50.             @Override
  51.             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
  52.                 super.onScrolled(recyclerView, dx, dy);
  53.                 totalItemCount = linearLayoutManager.getItemCount();
  54.                 lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
  55.                 if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
  56.                     isLoading = true;
  57.                     if (onLoadMoreListener != null) {
  58.                         onLoadMoreListener.onLoadMore();
  59.                     }
  60.                 }
  61.             }
  62.         });
  63.     }
  64.  
  65.     @NonNull
  66.     @Override
  67.     public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
  68.         if (i == VIEW_TYPE_ITEM) {
  69.             View v = LayoutInflater.from(context).inflate(R.layout.row_film_pria,null,false);
  70.             v.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
  71.             FilmPriaHolder fph = new FilmPriaHolder(v);
  72.             return fph;
  73.         } else if (i == VIEW_TYPE_LOADING){
  74.             View v = LayoutInflater.from(context).inflate(R.layout.footer_progressbar,null,false);
  75.             LoadingViewHolder lvh = new LoadingViewHolder(v);
  76.             return lvh;
  77.         }
  78.         return null;
  79.     }
  80.  
  81.     @Override
  82.     public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
  83.         if (holder instanceof FilmPriaHolder) {
  84.             FilmPriaHolder filmPriaHolder = (FilmPriaHolder) holder;
  85.             filmPriaHolder.tvArtistName.setText(filmPriaList.get(i).getArtistName());
  86.             filmPriaHolder.tvAge.setText("Usia : "+filmPriaList.get(i).getAge());
  87.             filmPriaHolder.tvHeight.setText("Tinggi : "+filmPriaList.get(i).getHeight());
  88.             filmPriaHolder.tvWeight.setText("Berat : "+filmPriaList.get(i).getWeight());
  89.             filmPriaHolder.tvDomicile.setText("Domisili : "+filmPriaList.get(i).getDomicile());
  90.             Glide.with(context)
  91.                     .load("http://192.168.0.41:8000/images/"+filmPriaList.get(i).getPhoto())
  92.                     .apply(RequestOptions.circleCropTransform())
  93.                     .error(R.drawable.pria)
  94.                     .placeholder(R.drawable.pria)
  95.                     .into(filmPriaHolder.ivArtistName);
  96.         } else if (holder instanceof LoadingViewHolder) {
  97.             LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
  98.             loadingViewHolder.footerProgressBar.setIndeterminate(true);
  99.         }
  100.     }
  101.  
  102.     @Override
  103.     public int getItemCount() {
  104.         //return filmPriaList.size();
  105.         return filmPriaList == null ? 0 : filmPriaList.size();
  106.     }
  107.  
  108.     public void setLoaded() {
  109.         isLoading = false;
  110.     }
  111.  
  112.     public class LoadingViewHolder extends RecyclerView.ViewHolder {
  113.         @BindView(R.id.footerProgressBar) ProgressBar footerProgressBar;
  114.  
  115.         public LoadingViewHolder(@NonNull View itemView) {
  116.             super(itemView);
  117.             ButterKnife.bind(this, itemView);
  118.         }
  119.     }
  120.  
  121.     public class FilmPriaHolder extends RecyclerView.ViewHolder {
  122.  
  123.         @BindView(R.id.tvArtistName) TextView tvArtistName;
  124.         @BindView(R.id.tvAge) TextView tvAge;
  125.         @BindView(R.id.tvHeight) TextView tvHeight;
  126.         @BindView(R.id.tvWeight) TextView tvWeight;
  127.         @BindView(R.id.tvDomicile) TextView tvDomicile;
  128.         @BindView(R.id.ivArtistName) ImageView ivArtistName;
  129.         public FilmPriaHolder(@NonNull View itemView) {
  130.             super(itemView);
  131.             ButterKnife.bind(this, itemView);
  132.         }
  133.     }
  134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top