SHARE
TWEET

Untitled

DmitryShupegin Jul 16th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.recyclerviewstest;
  2.  
  3. import android.content.Context;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.TextView;
  10.  
  11. import java.security.AccessController;
  12.  
  13. public class NumberAdapter extends RecyclerView.Adapter<NumberAdapter.NumberViewHolder> {
  14.     private int numberItems;
  15.     private static int viewHolderCount;
  16.  
  17.  
  18.     public NumberAdapter(int numberOfItems){
  19.         viewHolderCount = 0;
  20.         numberItems = numberOfItems;
  21.     }
  22.  
  23.     @NonNull
  24.     @Override
  25.     public NumberViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  26.  
  27.         Context context = parent.getContext();
  28.         int layoutIdForListItem = R.layout.number_list_item;
  29.  
  30.         LayoutInflater inflate = LayoutInflater.from(context);
  31.         View view = inflate.inflate(layoutIdForListItem,parent,false);
  32.         NumberViewHolder viewHolder = new NumberViewHolder(view);
  33.         viewHolder.viewHolderIndex.setText("ViewHolder index: " + viewHolderCount );
  34.         viewHolderCount++;
  35.         return viewHolder;
  36.     }
  37.  
  38.     @Override
  39.     public void onBindViewHolder(@NonNull NumberViewHolder holder, int position) {
  40.         holder.bind(position);
  41.  
  42.     }
  43.  
  44.     @Override
  45.     public int getItemCount() {
  46.         return numberItems;
  47.     }
  48.  
  49.     class NumberViewHolder  extends RecyclerView.ViewHolder{
  50.         TextView listItemNumberView;
  51.         TextView viewHolderIndex;
  52.         public NumberViewHolder(@NonNull View itemView) {
  53.             super(itemView);
  54.             listItemNumberView = itemView.findViewById(R.id.tv_number_item);
  55.             viewHolderIndex = itemView.findViewById(R.id.tv_number_view_holder);
  56.         }
  57.         void bind(int listIndex){
  58.             listItemNumberView.setText(String.valueOf(listIndex));
  59.         }
  60.     }
  61. }
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