FileDocCategorySizeDatePackage
PresenterSwitcher.javaAPI DocAndroid 5.1 API3394Thu Mar 12 22:22:56 GMT 2015android.support.v17.leanback.widget

PresenterSwitcher

public abstract class PresenterSwitcher extends Object
An abstract helper class that switches view in parent view using {@link PresenterSelector} subclass should define {@link #insertView(View)} of how to add the view in parent and optionally override {@link #onViewSelected(View)}.

Fields Summary
private android.view.ViewGroup
mParent
private PresenterSelector
mPresenterSelector
private Presenter
mCurrentPresenter
private Presenter.ViewHolder
mCurrentViewHolder
Constructors Summary
Methods Summary
public voidclear()
Destroy created views.

        if (mCurrentPresenter != null) {
            mCurrentPresenter.onUnbindViewHolder(mCurrentViewHolder);
            mParent.removeView(mCurrentViewHolder.view);
            mCurrentViewHolder = null;
            mCurrentPresenter = null;
        }
    
public final android.view.ViewGroupgetParentViewGroup()

        return mParent;
    
public voidinit(android.view.ViewGroup parent, PresenterSelector presenterSelector)
Initialize switcher with a parent view to insert view into and a {@link PresenterSelector} for choose {@link Presenter} for object. This will destroy any existing views.

        clear();
        mParent = parent;
        mPresenterSelector = presenterSelector;
    
protected abstract voidinsertView(android.view.View view)

protected voidonViewSelected(android.view.View view)
Called when a view is bound to the object of {@link #select(Object)}.

    
public voidselect(java.lang.Object object)

        switchView(object);
        showView(true);
    
private voidshowView(boolean show)

        if (mCurrentViewHolder != null) {
            showView(mCurrentViewHolder.view, show);
        }
    
protected voidshowView(android.view.View view, boolean visible)

        view.setVisibility(visible ? View.VISIBLE : View.GONE);
    
private voidswitchView(java.lang.Object object)

        Presenter presenter = mPresenterSelector.getPresenter(object);
        if (presenter != mCurrentPresenter) {
            showView(false);
            clear();
            mCurrentPresenter = presenter;
            if (mCurrentPresenter == null) {
                return;
            }
            mCurrentViewHolder = mCurrentPresenter.onCreateViewHolder(mParent);
            insertView(mCurrentViewHolder.view);
        } else {
            if (mCurrentPresenter == null) {
                return;
            }
            mCurrentPresenter.onUnbindViewHolder(mCurrentViewHolder);
        }
        mCurrentPresenter.onBindViewHolder(mCurrentViewHolder, object);
        onViewSelected(mCurrentViewHolder.view);
    
public voidunselect()

        showView(false);