package com.madisp.pretty;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

 * A small helper class to wrap a LayoutInflater's factory.
public abstract class LayoutFactoryWrapper implements LayoutInflater.Factory2 {
    private final LayoutInflater inflater;
    private final LayoutInflater.Factory2 base;

    // from
    private static final String[] CLASS_PREFIX_LIST = {

    public LayoutFactoryWrapper(@NotNull LayoutInflater inflater, @Nullable LayoutInflater.Factory2 baseFactory) {
        this.inflater = inflater;
        this.base = baseFactory;

    public View onCreateView(@Nullable View parent, @NotNull String name, @NotNull Context context, @NotNull AttributeSet attrs) {
        View v = null;
        if (base != null) {
            v = base.onCreateView(parent, name, context, attrs);
        if (v == null) {
            // construct a view the same way that android does it
            for (String prefix : CLASS_PREFIX_LIST) {
                try {
                    v = inflater.createView(name, prefix, attrs);
                    if (v != null) {
                } catch (ClassNotFoundException ignored) {
                    // let v be null, this means that we don't know how to inflate this view
        v = onViewCreated(v, parent, name, context, attrs);
        return v;

    public View onCreateView(@NotNull String name, @NotNull Context context, @NotNull AttributeSet attrs) {
        return onCreateView(null, name, context, attrs);

    protected abstract View onViewCreated(@Nullable View view, @Nullable View parent, @NotNull String name, @NotNull Context context, @NotNull AttributeSet attrs);