Advertisement
yo2man

Recycler View, getView() and the ViewHolder Pattern

Jul 17th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. // Recycler View, getView() and the ViewHolder Pattern
  2.  
  3. // This may sound a little silly, but one of the most important features of a list in an app is how smoothly it scrolls.
  4. /* What recycler view does is basically recycle the view containers so the app doesn't load the whole list of data, which can be like 100000 lists. Instead it loads the data as the user scrolls down. It "recycles" the container of the views that the users scroll past and use them for the upcoming views that the user is going to scroll to. */
  5.  
  6. package com.teamtreehouse.stormy.adapters;
  7.  
  8. import android.content.Context;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.BaseAdapter;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15.  
  16. import com.teamtreehouse.stormy.R;
  17. import com.teamtreehouse.stormy.weather.Day;
  18.  
  19. /**
  20.  * Created by Fu on 7/16/2015.
  21.  */
  22. public class DayAdapter extends BaseAdapter {
  23.  
  24.     //Step 1. This adapter needs to know the context and the data its going to map:
  25.     private Context mContext; //lets create Context and call it mContext
  26.     private Day[] mDays;  //and lets create a private Day array and call it mDays
  27.  
  28.     //Step 2. Now lets create a constructor that allows us to set these two values.
  29.     public DayAdapter(Context context, Day[] days){
  30.         mContext = context;
  31.         mDays = days;
  32.     }
  33.  
  34.     //Step 3. Next, move through these methods and implement them one by one
  35.  
  36.     @Override
  37.     //getCount, gets us the count of items in the array that this adapter is using. So that is just the count of the mDays array.
  38.     public int getCount() {
  39.         return mDays.length; //so we can use the property: mDays.length
  40.     }
  41.  
  42.     @Override
  43.     //getItem gets the item for the adapter given a certain position.
  44.     public Object getItem(int position) {
  45.         return mDays[position]; // So we'll do mDays and the item at position. <- get data from the mDays array again like in the previous method.
  46.     }
  47.  
  48.     @Override
  49.     //we aren't going to use ItemId. This method is used to Tag items for easy reference.
  50.     public long getItemId(int position) {
  51.         return 0;
  52.     }
  53.  
  54.     @Override
  55.     //getView is where the mapping occurs. //getView method is the method that is called for each item in the list. As it's prepared for the screen, the getView method is called. So its called when it is initially displayed and when we scroll a new item onto the list
  56.     //This is where we need to set the "data from the views" to the "data we provided from the adapter"
  57.     public View getView(int position, View convertView, ViewGroup parent) {  // the  (View convertView) parameter is the View object that we want to reuse[1]
  58.         ViewHolder holder;  //ViewHolder variable // <-*
  59.  
  60.         //[1] if this is the first time getViewed is called, the convertView will be null. If it is null, we need to create it and set it up.
  61.         // But if it's not null, then we have a view already available to us to use, and all we need to do is reset the data. That's where the magic recycling happens.
  62.  
  63.         if (convertView == null) { //that means its brand new and we need to create everything
  64.             convertView = LayoutInflater.form(mContext).inflate(R.layout.daily_list_item, null); //layout inflater is an android object that takes xml layouts and turns them into views in code that we then can use. //Don't worry we can just copy this whenever we need it.
  65.  
  66.             //lets initiated that holder variable we set in line 53: "ViewHolder holder;"
  67.             // And now we can set it just like we said views in activities onCreate method.
  68.             holder = new ViewHolder;
  69.             holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
  70.  
  71.             //do the same thing for temperature view label
  72.             holder.temperatureLabel = (TextView) convertView.findViewById(R.id.temperatureLabel);
  73.  
  74.             //and do the same thing for day Label
  75.             holder.dayLabel = (TextView) convertView.findViewById(R.id.dayNameLabel);
  76.  
  77.  
  78.             convertView.setTag(holder); //type convertView and then use the holder variable as the tag. //this sets a tag for the View that we will reuse in a moment
  79.         }
  80.         else {
  81.             // And if it is this case where we already have all of these views set up.
  82.             // We can just set our view holder based on the tag we just set.
  83.                 holder = (ViewHolder) convertView.getTag(); //cast to a (ViewHolder) then convertView and call .getTag //// So, because the holder is associated with the view, if we call getTag,
  84.             // where we set it as the tag, and we just need to cast it as a view holder class
  85.             }
  86.  
  87.  
  88.         }
  89.         return null;
  90.     }
  91.  
  92.     //We will start though by using a special helper class, called a view holder.
  93.     // this helper object associated with this view let's us re-use the same references to objects like textViews and ImageViews which saves us memory and power.
  94.     //Anyhow, we add a custom viewholder class as a nested class right in here.
  95.     private static class ViewHolder { //our ViewHolder class going to be exactly what it sounds like: hold views we added to our list item layout.
  96.  
  97.         //these are going to be public by default
  98.         ImageView iconImageView;
  99.         TextView temperatureLabel;
  100.         TextView dayLabel;
  101.     } //now we wanna go up and create a holder variable in our getViewMethod //*
  102. }
  103.  
  104. //Let's take a short break and we'll come back and set the data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement