/* * Copyright (C) 2015 The Android Open Source Project * * 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. */ package android.support.v17.leanback.supportleanbackshowcase.cards.presenters; import android.content.Context; import android.support.v17.leanback.supportleanbackshowcase.models.Card; import android.support.v17.leanback.widget.BaseCardView; import android.support.v17.leanback.widget.Presenter; import android.view.ViewGroup; /** * This abstract, generic class will create and manage the * ViewHolder and will provide typed Presenter callbacks such that you do not have to perform casts * on your own. * * @param <T> View type for the card. */ public abstract class AbstractCardPresenter<T extends BaseCardView> extends Presenter { private static final String TAG = "AbstractCardPresenter"; private final Context mContext; /** * @param context The current context. */ public AbstractCardPresenter(Context context) { mContext = context; } public Context getContext() { return mContext; } @Override public final ViewHolder onCreateViewHolder(ViewGroup parent) { T cardView = onCreateView(); return new ViewHolder(cardView); } @Override public final void onBindViewHolder(ViewHolder viewHolder, Object item) { Card card = (Card) item; onBindViewHolder(card, (T) viewHolder.view); } @Override public final void onUnbindViewHolder(ViewHolder viewHolder) { onUnbindViewHolder((T) viewHolder.view); } public void onUnbindViewHolder(T cardView) { // Nothing to clean up. Override if necessary. } /** * Invoked when a new view is created. * * @return Returns the newly created view. */ protected abstract T onCreateView(); /** * Implement this method to update your card's view with the data bound to it. * * @param card The model containing the data for the card. * @param cardView The view the card is bound to. * @see Presenter#onBindViewHolder(Presenter.ViewHolder, Object) */ public abstract void onBindViewHolder(Card card, T cardView); }