Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Implementing ViewHolder

Tags:

java

android

I am trying to implement ViewHolder in my Android app, but I keep getting that ViewHolder cannot be resolved to a type, without any suggestions for an import. Anyone know how to get around this?

like image 489
John Roberts Avatar asked Dec 30 '12 01:12

John Roberts


People also ask

What is the ViewHolder in Android?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.

Why do we use ViewHolder in Android?

The ViewHolder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent call of findViewById() during ListView scrolling, and that will make it smooth.

How do I create a view holder?

You define the view holder by extending RecyclerView.ViewHolder . The RecyclerView requests those views, and binds the views to their data, by calling methods in the adapter. You define the adapter by extending RecyclerView.Adapter . The layout manager arranges the individual elements in your list.

How does a ViewHolder work?

ViewHolder: ViewHolder is a type of helper class that allows us to draw the UI for individual items on the screen. LayoutManager: LayoutManager in recyclerView assists us in determining how to display the items on the screen. It can be done either linearly or in a grid.


2 Answers

That's because a ViewHolder is not a class that is from the Android SDK, you make it yourself. Based on what I can find, a ViewHolder is an implementation that stores Views (per row in a ListView usually) for a larger area, so it is a sort of helper class and cache mechanism. This is one example I found on Android Developers of what a ViewHolder would contain.

static class ViewHolder {
  TextView text;
  TextView timestamp;
  ImageView icon;
  ProgressBar progress;
  int position;
}

Then you can implement it in a ListAdapter or a similar class.

like image 67
A--C Avatar answered Oct 16 '22 23:10

A--C


**Create a Holder class**

protected static class ViewHolderItems
{
    private ImageView mStoreImage;
    private TextView mStoreName;
}

And use In getView method of adapter

 public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolderItems viewHolder;
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.coupen_row, null);
            viewHolder = new ViewHolderItems();
            viewHolder.mStoreImage = (ImageView) convertView.findViewById(R.id.storeImage);

            viewHolder.mStoreName = (TextView) convertView.findViewById(R.id.storeName);

            convertView.setTag(viewHolder);

        } 
       else 
        {
            viewHolder = (ViewHolderItems) convertView.getTag();
        }

         return convertView;
    }
like image 44
Droid_Mechanic Avatar answered Oct 16 '22 23:10

Droid_Mechanic