obtain View child by id - Android User Interface

Android examples for User Interface:View Child

Description

obtain View child by id

Demo Code


//package com.java2s;

import android.util.SparseArray;
import android.view.View;

public class Main {

    @SuppressWarnings("unchecked")
    public static <T extends View> T obtainView(View convertView, int id) {
        SparseArray<View> holder = (SparseArray<View>) convertView.getTag();
        if (holder == null) {
            holder = new SparseArray<View>();
            convertView.setTag(holder);// w w w .  j a v  a  2s .  c  o m
        }
        View childView = holder.get(id);
        if (childView == null) {
            childView = convertView.findViewById(id);
            holder.put(id, childView);
        }
        return (T) childView;
    }
}

Related Tutorials