Pages

Thursday, April 9, 2015

Android ViewHolder Basic example

What is View Holder and why we use it?
      The View Holder 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 List View scrolling, and that will make it smooth.

1) Take List View in you activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ListView android:id="@+id/list"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:transcriptMode="alwaysScroll"
              android:listSelector="@android:color/transparent"/>
</LinearLayout>


2) Create list_item.xml for list item


 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list_item"
        android:gravity="center_vertical">
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/list_item_text_view"
              android:textSize="20sp"
              android:padding="10dp"
              android:layout_marginLeft="5dp"/>
</LinearLayout>



3) Create Custom Adapter for ViewHoder to set text in TextView "CustomAdapter.java"


package com.example;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends BaseAdapter {
    private ArrayList<String> mListItems;
    private LayoutInflater mLayoutInflater;
    public CustomAdapter(Context context, ArrayList<String> arrayList){
        mListItems = arrayList;
        //get the layout inflater
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        //getCount() represents how many items are in the list
        return mListItems.size();
    }
    @Override
    //get the data of an item from a specific position
    //i represents the position of the item in the list
    public Object getItem(int i) {
        return null;
    }
    @Override
    //get the position id of the item from the list
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = mLayoutInflater.inflate(R.layout.list_item, null);
            holder.itemName = (TextView) view.findViewById(R.id.list_item_text_view);
        } else {
            holder = (ViewHolder)view.getTag();
        }
        String stringItem = mListItems.get(position);
        if (stringItem != null) {
            if (holder.itemName != null) {
                //set the item name on the TextView
                holder.itemName.setText(stringItem);
            }
        }
        return view;
    }
    private static class ViewHolder {
        protected TextView itemName;
    }
}


4) MainActivity .java

package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MyActivity extends Activity
{
    private ListView myList;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<String> list = new ArrayList();
        for (int i = 0; i < 20; i++){
            list.add("Odoo" + i);
        }
        myList = (ListView)findViewById(R.id.list);
ning the data backing this list and for producing
   
        myList.setAdapter(new CustomAdapter(MyActivity.this,list));
    }
}


Output