Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import android.support.annotation.NonNull;
  2. import android.support.v7.widget.RecyclerView;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6.  
  7. import java.lang.reflect.Constructor;
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.util.ArrayList;
  10.  
  11. abstract public class BaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH>{
  12.  
  13. @NonNull
  14. public ArrayList<T> dataList = new ArrayList<>();
  15. protected int mModelLayout;
  16. Class<VH> mViewHolderClass;
  17.  
  18. public BaseRecyclerAdapter(int modelLayout, Class<VH> viewHolderClass){
  19. mModelLayout = modelLayout;
  20. mViewHolderClass = viewHolderClass;
  21. }
  22.  
  23. @Override
  24. public VH onCreateViewHolder(ViewGroup parent, int viewType) {
  25. ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(mModelLayout, parent, false);
  26. try {
  27. Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class);
  28. VH vh = constructor.newInstance(view);
  29. onCreateAfterViewHolder(vh);
  30. return vh;
  31. } catch (NoSuchMethodException e) {
  32. throw new RuntimeException(e);
  33. } catch (InvocationTargetException e) {
  34. throw new RuntimeException(e);
  35. } catch (InstantiationException e) {
  36. throw new RuntimeException(e);
  37. } catch (IllegalAccessException e) {
  38. throw new RuntimeException(e);
  39. }
  40. }
  41.  
  42. public void onCreateAfterViewHolder(VH holder){}
  43.  
  44. @Override
  45. public void onBindViewHolder(VH holder, int position) {
  46. dataConvertViewHolder(holder,dataList.get(position));
  47. }
  48.  
  49. abstract public void dataConvertViewHolder(VH holder, T data);
  50. @Override
  51. public int getItemCount() {
  52. return dataList.size();
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement