147520b68e50572a9775a662410c5aff8300c8784Craig Stout/* 247520b68e50572a9775a662410c5aff8300c8784Craig Stout * Copyright (C) 2014 The Android Open Source Project 347520b68e50572a9775a662410c5aff8300c8784Craig Stout * 447520b68e50572a9775a662410c5aff8300c8784Craig Stout * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 547520b68e50572a9775a662410c5aff8300c8784Craig Stout * in compliance with the License. You may obtain a copy of the License at 647520b68e50572a9775a662410c5aff8300c8784Craig Stout * 747520b68e50572a9775a662410c5aff8300c8784Craig Stout * http://www.apache.org/licenses/LICENSE-2.0 847520b68e50572a9775a662410c5aff8300c8784Craig Stout * 947520b68e50572a9775a662410c5aff8300c8784Craig Stout * Unless required by applicable law or agreed to in writing, software distributed under the License 1047520b68e50572a9775a662410c5aff8300c8784Craig Stout * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 1147520b68e50572a9775a662410c5aff8300c8784Craig Stout * or implied. See the License for the specific language governing permissions and limitations under 1247520b68e50572a9775a662410c5aff8300c8784Craig Stout * the License. 1347520b68e50572a9775a662410c5aff8300c8784Craig Stout */ 1447520b68e50572a9775a662410c5aff8300c8784Craig Stoutpackage android.support.v17.leanback.widget; 1547520b68e50572a9775a662410c5aff8300c8784Craig Stout 1647520b68e50572a9775a662410c5aff8300c8784Craig Stoutimport android.view.View; 1747520b68e50572a9775a662410c5aff8300c8784Craig Stoutimport android.view.ViewGroup; 1847520b68e50572a9775a662410c5aff8300c8784Craig Stout 1947520b68e50572a9775a662410c5aff8300c8784Craig Stout/** 20a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * An abstract helper class that switches a view in its parent view using a 21a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * {@link PresenterSelector}. A subclass should implement {@link #insertView(View)} to define 22a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * how to add the view in parent, and may optionally override {@link #onViewSelected(View)}. 2347520b68e50572a9775a662410c5aff8300c8784Craig Stout */ 2447520b68e50572a9775a662410c5aff8300c8784Craig Stoutpublic abstract class PresenterSwitcher { 2547520b68e50572a9775a662410c5aff8300c8784Craig Stout 2647520b68e50572a9775a662410c5aff8300c8784Craig Stout private ViewGroup mParent; 2747520b68e50572a9775a662410c5aff8300c8784Craig Stout private PresenterSelector mPresenterSelector; 2847520b68e50572a9775a662410c5aff8300c8784Craig Stout private Presenter mCurrentPresenter; 2947520b68e50572a9775a662410c5aff8300c8784Craig Stout private Presenter.ViewHolder mCurrentViewHolder; 3047520b68e50572a9775a662410c5aff8300c8784Craig Stout 3147520b68e50572a9775a662410c5aff8300c8784Craig Stout /** 32a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Initializes the switcher with a parent view to insert view into and a 33a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * {@link PresenterSelector} for choosing a {@link Presenter} for a given object. 3447520b68e50572a9775a662410c5aff8300c8784Craig Stout * This will destroy any existing views. 3547520b68e50572a9775a662410c5aff8300c8784Craig Stout */ 3647520b68e50572a9775a662410c5aff8300c8784Craig Stout public void init(ViewGroup parent, PresenterSelector presenterSelector) { 3747520b68e50572a9775a662410c5aff8300c8784Craig Stout clear(); 3847520b68e50572a9775a662410c5aff8300c8784Craig Stout mParent = parent; 3947520b68e50572a9775a662410c5aff8300c8784Craig Stout mPresenterSelector = presenterSelector; 4047520b68e50572a9775a662410c5aff8300c8784Craig Stout } 4147520b68e50572a9775a662410c5aff8300c8784Craig Stout 42a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout /** 43a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Selects a view based on the given object and shows that view. 44a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout */ 4547520b68e50572a9775a662410c5aff8300c8784Craig Stout public void select(Object object) { 4647520b68e50572a9775a662410c5aff8300c8784Craig Stout switchView(object); 4747520b68e50572a9775a662410c5aff8300c8784Craig Stout showView(true); 4847520b68e50572a9775a662410c5aff8300c8784Craig Stout } 4947520b68e50572a9775a662410c5aff8300c8784Craig Stout 50a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout /** 51a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Hides the view. 52a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout */ 5347520b68e50572a9775a662410c5aff8300c8784Craig Stout public void unselect() { 5447520b68e50572a9775a662410c5aff8300c8784Craig Stout showView(false); 5547520b68e50572a9775a662410c5aff8300c8784Craig Stout } 5647520b68e50572a9775a662410c5aff8300c8784Craig Stout 57a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout /** 58a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Returns the parent. 59a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout */ 6047520b68e50572a9775a662410c5aff8300c8784Craig Stout public final ViewGroup getParentViewGroup() { 6147520b68e50572a9775a662410c5aff8300c8784Craig Stout return mParent; 6247520b68e50572a9775a662410c5aff8300c8784Craig Stout } 6347520b68e50572a9775a662410c5aff8300c8784Craig Stout 6447520b68e50572a9775a662410c5aff8300c8784Craig Stout private void showView(boolean show) { 6547520b68e50572a9775a662410c5aff8300c8784Craig Stout if (mCurrentViewHolder != null) { 6647520b68e50572a9775a662410c5aff8300c8784Craig Stout showView(mCurrentViewHolder.view, show); 6747520b68e50572a9775a662410c5aff8300c8784Craig Stout } 6847520b68e50572a9775a662410c5aff8300c8784Craig Stout } 6947520b68e50572a9775a662410c5aff8300c8784Craig Stout 7047520b68e50572a9775a662410c5aff8300c8784Craig Stout private void switchView(Object object) { 7147520b68e50572a9775a662410c5aff8300c8784Craig Stout Presenter presenter = mPresenterSelector.getPresenter(object); 7247520b68e50572a9775a662410c5aff8300c8784Craig Stout if (presenter != mCurrentPresenter) { 7347520b68e50572a9775a662410c5aff8300c8784Craig Stout showView(false); 7447520b68e50572a9775a662410c5aff8300c8784Craig Stout clear(); 7547520b68e50572a9775a662410c5aff8300c8784Craig Stout mCurrentPresenter = presenter; 7647520b68e50572a9775a662410c5aff8300c8784Craig Stout if (mCurrentPresenter == null) { 7747520b68e50572a9775a662410c5aff8300c8784Craig Stout return; 7847520b68e50572a9775a662410c5aff8300c8784Craig Stout } 7947520b68e50572a9775a662410c5aff8300c8784Craig Stout mCurrentViewHolder = mCurrentPresenter.onCreateViewHolder(mParent); 8047520b68e50572a9775a662410c5aff8300c8784Craig Stout insertView(mCurrentViewHolder.view); 8147520b68e50572a9775a662410c5aff8300c8784Craig Stout } else { 8247520b68e50572a9775a662410c5aff8300c8784Craig Stout if (mCurrentPresenter == null) { 8347520b68e50572a9775a662410c5aff8300c8784Craig Stout return; 8447520b68e50572a9775a662410c5aff8300c8784Craig Stout } 8547520b68e50572a9775a662410c5aff8300c8784Craig Stout mCurrentPresenter.onUnbindViewHolder(mCurrentViewHolder); 8647520b68e50572a9775a662410c5aff8300c8784Craig Stout } 8747520b68e50572a9775a662410c5aff8300c8784Craig Stout mCurrentPresenter.onBindViewHolder(mCurrentViewHolder, object); 8847520b68e50572a9775a662410c5aff8300c8784Craig Stout onViewSelected(mCurrentViewHolder.view); 8947520b68e50572a9775a662410c5aff8300c8784Craig Stout } 9047520b68e50572a9775a662410c5aff8300c8784Craig Stout 9147520b68e50572a9775a662410c5aff8300c8784Craig Stout protected abstract void insertView(View view); 9247520b68e50572a9775a662410c5aff8300c8784Craig Stout 9347520b68e50572a9775a662410c5aff8300c8784Craig Stout /** 9447520b68e50572a9775a662410c5aff8300c8784Craig Stout * Called when a view is bound to the object of {@link #select(Object)}. 9547520b68e50572a9775a662410c5aff8300c8784Craig Stout */ 9647520b68e50572a9775a662410c5aff8300c8784Craig Stout protected void onViewSelected(View view) { 9747520b68e50572a9775a662410c5aff8300c8784Craig Stout } 9847520b68e50572a9775a662410c5aff8300c8784Craig Stout 9947520b68e50572a9775a662410c5aff8300c8784Craig Stout protected void showView(View view, boolean visible) { 10047520b68e50572a9775a662410c5aff8300c8784Craig Stout view.setVisibility(visible ? View.VISIBLE : View.GONE); 10147520b68e50572a9775a662410c5aff8300c8784Craig Stout } 10247520b68e50572a9775a662410c5aff8300c8784Craig Stout 10347520b68e50572a9775a662410c5aff8300c8784Craig Stout /** 104a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * Destroys created views. 10547520b68e50572a9775a662410c5aff8300c8784Craig Stout */ 10647520b68e50572a9775a662410c5aff8300c8784Craig Stout public void clear() { 10747520b68e50572a9775a662410c5aff8300c8784Craig Stout if (mCurrentPresenter != null) { 10847520b68e50572a9775a662410c5aff8300c8784Craig Stout mCurrentPresenter.onUnbindViewHolder(mCurrentViewHolder); 10947520b68e50572a9775a662410c5aff8300c8784Craig Stout mParent.removeView(mCurrentViewHolder.view); 11047520b68e50572a9775a662410c5aff8300c8784Craig Stout mCurrentViewHolder = null; 11147520b68e50572a9775a662410c5aff8300c8784Craig Stout mCurrentPresenter = null; 11247520b68e50572a9775a662410c5aff8300c8784Craig Stout } 11347520b68e50572a9775a662410c5aff8300c8784Craig Stout } 11447520b68e50572a9775a662410c5aff8300c8784Craig Stout 11547520b68e50572a9775a662410c5aff8300c8784Craig Stout} 116