Convenience wrapper over #recycle(android.view.View,android.view.ViewGroup,int) . - Android User Interface

Android examples for User Interface:RecyclerView

Description

Convenience wrapper over #recycle(android.view.View,android.view.ViewGroup,int) .

Demo Code

/*/* w  ww  .  j a va2 s . c o  m*/
 * Copyright 2014 Lorenzo Villani
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Main{
    /**
     * Convenience wrapper over {@link #recycle(android.view.View, android.view.ViewGroup, int)}.
     * <p/>
     * Just like {@link #recycle(android.view.View, android.view.ViewGroup, int)}, the
     * {@link android.view.LayoutInflater} is automatically obtained from the global application
     * object but the parent view is to {@code null}.
     *
     * @param convertView The old view to reuse, if possible. If null, a new one is created with the
     *                    given layout id.
     * @param layoutId    The ID of the layout to inflate if needed.
     * @param <T>         The return type.
     * @return The recycled view, if possible, or a newly created one.
     * @throws ClassCastException When the convertView can't be downcast to the lvalue's type.
     */
    @SuppressWarnings("unchecked")
    public static <T> T recycle(final View convertView, int layoutId) {
        return recycle(Application.getLayoutInflater(), convertView, null,
                layoutId);
    }
    /**
     * Convenience wrapper over {@link #recycle(android.view.LayoutInflater, android.view.View, android.view.ViewGroup, int)}.
     * <p/>
     * The {@link android.view.LayoutInflater} is automatically obtained from the global application
     * object which must inherit from {@link me.villani.lorenzo.droidkit.Application}.
     *
     * @param convertView The old view to reuse, if possible. If null, a new one is created with the
     *                    given layout id.
     * @param parent      The parent the view will eventually be attached to.
     * @param layoutId    The ID of the layout to inflate if needed.
     * @param <T>         The return type.
     * @return The recycled view, if possible, or a newly created one.
     * @throws ClassCastException When the convertView can't be downcast to the lvalue's type.
     */
    @SuppressWarnings("unchecked")
    public static <T> T recycle(final View convertView,
            final ViewGroup parent, int layoutId) {
        return recycle(Application.getLayoutInflater(), convertView,
                parent, layoutId);
    }
    /**
     * Helper method to recycle a view in a ListView adapter.
     *
     * @param inflater    The {@link android.view.LayoutInflater} to use if a fresh View is needed.
     * @param convertView The old view to reuse, if possible. If null, a new one is created using
     *                    the given {@link android.view.LayoutInflater} and layoutId.
     * @param parent      The parent the view will eventually be attached to.
     * @param layoutId    The ID of the layout to inflate if needed.
     * @param <T>         The return type.
     * @return The recycled view, if possible, or a newly created one.
     * @throws ClassCastException When the convertView can't be downcast to the lvalue's type.
     */
    @SuppressWarnings("unchecked")
    public static <T> T recycle(final LayoutInflater inflater,
            final View convertView, final ViewGroup parent, int layoutId) {
        if (convertView == null) {
            return (T) inflater.inflate(layoutId, parent);
        } else {
            return (T) convertView;
        }
    }
}

Related Tutorials