ViewHolder Pattern in Android


Create new project in Android Studio with steps as below:




Open res\values\strings.xml file and add new string as below:

<resources>
    <string name="app_name">Learn Android with Real Apps</string>
    <string name="usd">$</string>
</resources>

Copy images to res\drawable folder

Create new package named entities. In this package, create new java class named Product.java as below:

package android.demo.entities;

import java.io.Serializable;

public class Product implements Serializable {

    private String id;

    private String name;

    private double price;

    private String description;

    private int photo;

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getPhoto() {
        return this.photo;
    }

    public void setPhoto(int photo) {
        this.photo = photo;
    }

    public Product() {
    }

    public Product(String id, String name, double price, String description, int photo) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.description = description;
        this.photo = photo;
    }
}




Select res\layout folder. In this folder, create new layout named product_list_layout.xml as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageViewPhoto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitStart"
        app:srcCompat="@drawable/thumb1" />

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textViewDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textViewPrice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Create new package named adapters. In this package, create new java class named ProductListAdapter.java as below:

package android.demo.adapters;

import android.content.Context;
import android.demo.entities.Product;
import android.demo.learnandroidwithrealapps.R;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class ProductListAdapter extends ArrayAdapter<Product> {

    private Context context;
    private List<Product> products;

    public ProductListAdapter(Context context, List<Product> products) {
        super(context, R.layout.product_list_layout, products);
        this.context = context;
        this.products = products;
    }

    @NonNull
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.product_list_layout, parent, false);
            viewHolder.textViewName = view.findViewById(R.id.textViewName);
            viewHolder.textViewPrice = view.findViewById(R.id.textViewPrice);
            viewHolder.textViewDescription = view.findViewById(R.id.textViewDescription);
            viewHolder.imageViewPhoto = view.findViewById(R.id.imageViewPhoto);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Product product = products.get(position);
        viewHolder.textViewName.setText(product.getName());
        viewHolder.textViewDescription.setText(product.getDescription());
        viewHolder.textViewPrice.setText(context.getText(R.string.usd) + String.valueOf(product.getPrice()));
        viewHolder.imageViewPhoto.setImageResource(product.getPhoto());
        return view;
    }

    private static class ViewHolder {
        public static TextView textViewName;
        public static TextView textViewDescription;
        public static TextView textViewPrice;
        public static ImageView imageViewPhoto;
    }

}




Open res\layout\activity_main.xml file and create layout as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/gridViewProduct"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3" />

</LinearLayout>

Open MainActivity.java in android.demo.learnandroidwithrealapps package. Fill data to GridView control and use events for controls as below:

package android.demo.learnandroidwithrealapps;

import android.demo.adapters.ProductListAdapter;
import android.demo.entities.Product;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private GridView gridViewProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        loadData();
    }

    private void initView() {
        gridViewProduct = findViewById(R.id.gridViewProduct);
        gridViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                gridViewProduct_onItemClick(adapterView, view, i, l);
            }
        });
    }

    private void loadData() {
        List<Product> products = new ArrayList<Product>();
        products.add(new Product("p01", "Name 1", 4, "Description for Product 1", R.drawable.thumb1));
        products.add(new Product("p02", "Name 2", 8, "Description for Product 2", R.drawable.thumb2));
        products.add(new Product("p03", "Name 3", 9, "Description for Product 3", R.drawable.thumb3));
        products.add(new Product("p04", "Name 4", 11, "Description for Product 4", R.drawable.thumb1));
        products.add(new Product("p05", "Name 5", 5, "Description for Product 5", R.drawable.thumb2));
        products.add(new Product("p06", "Name 6", 21, "Description for Product 6", R.drawable.thumb3));
        products.add(new Product("p07", "Name 7", 15, "Description for Product 7", R.drawable.thumb1));
        products.add(new Product("p08", "Name 8", 8, "Description for Product 8", R.drawable.thumb2));
        products.add(new Product("p09", "Name 9", 32, "Description for Product 9", R.drawable.thumb3));
        ProductListAdapter productListAdapter = new ProductListAdapter(getApplicationContext(), products);
        gridViewProduct.setAdapter(productListAdapter);
    }

    private void gridViewProduct_onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Product product = (Product) adapterView.getItemAtPosition(i);
        Toast.makeText(getApplicationContext(), product.getName(), Toast.LENGTH_LONG).show();
    }

}