public final class

HorizontalHoverCardSwitcher

extends PresenterSwitcher

 java.lang.Object

androidx.leanback.widget.PresenterSwitcher

↳androidx.leanback.widget.HorizontalHoverCardSwitcher

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback', version: '1.2.0-alpha02'

  • groupId: androidx.leanback
  • artifactId: leanback
  • version: 1.2.0-alpha02

Artifact androidx.leanback:leanback:1.2.0-alpha02 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback com.android.support:leanback-v17

Androidx class mapping:

androidx.leanback.widget.HorizontalHoverCardSwitcher android.support.v17.leanback.widget.HorizontalHoverCardSwitcher

Overview

A helper class for showing a hover card view below a HorizontalGridView. The hover card is aligned to the starting edge of the selected child view. If there is no space when scrolling to the end, the ending edge of the hover card will be aligned to the ending edge of the parent view, excluding padding.

Summary

Constructors
publicHorizontalHoverCardSwitcher()

Methods
protected voidinsertView(View view)

protected voidonViewSelected(View view)

Called when a view is bound to the object of PresenterSwitcher.select(Object).

public voidselect(HorizontalGridView gridView, View childView, java.lang.Object object)

Select a childView inside a grid view and create/bind a corresponding hover card view for the object.

from PresenterSwitcherclear, getParentViewGroup, init, select, showView, unselect
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public HorizontalHoverCardSwitcher()

Methods

protected void insertView(View view)

protected void onViewSelected(View view)

Called when a view is bound to the object of PresenterSwitcher.select(Object).

public void select(HorizontalGridView gridView, View childView, java.lang.Object object)

Select a childView inside a grid view and create/bind a corresponding hover card view for the object.

Source

/*
 * Copyright (C) 2014 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 androidx.leanback.widget;

import android.graphics.Rect;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;

import androidx.core.view.ViewCompat;

/**
 * A helper class for showing a hover card view below a {@link HorizontalGridView}.  The hover card
 * is aligned to the starting edge of the selected child view.  If there is no space when scrolling
 * to the end, the ending edge of the hover card will be aligned to the ending edge of the parent
 * view, excluding padding.
 */
public final class HorizontalHoverCardSwitcher extends PresenterSwitcher {
    // left and right of selected card view
    int mCardLeft, mCardRight;

    private int[] mTmpOffsets = new int[2];
    private Rect mTmpRect = new Rect();

    @Override
    protected void insertView(View view) {
        // append hovercard to the end of container
        getParentViewGroup().addView(view);
    }

    @Override
    protected void onViewSelected(View view) {
        int rightLimit = getParentViewGroup().getWidth() - getParentViewGroup().getPaddingRight();
        int leftLimit = getParentViewGroup().getPaddingLeft();
        // measure the hover card width; if it's too large, align hover card
        // end edge with row view's end edge, otherwise align start edges.
        view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
        boolean isRtl = ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
        if (!isRtl && mCardLeft + view.getMeasuredWidth() > rightLimit) {
            params.leftMargin = rightLimit  - view.getMeasuredWidth();
        } else if (isRtl && mCardLeft < leftLimit) {
            params.leftMargin = leftLimit;
        } else if (isRtl) {
            params.leftMargin = mCardRight - view.getMeasuredWidth();
        } else {
            params.leftMargin = mCardLeft;
        }
        view.requestLayout();
    }

    /**
     * Select a childView inside a grid view and create/bind a corresponding hover card view
     * for the object.
     */
    public void select(HorizontalGridView gridView, View childView, Object object) {
        ViewGroup parent = getParentViewGroup();
        gridView.getViewSelectedOffsets(childView, mTmpOffsets);
        mTmpRect.set(0, 0, childView.getWidth(), childView.getHeight());
        parent.offsetDescendantRectToMyCoords(childView, mTmpRect);
        mCardLeft = mTmpRect.left - mTmpOffsets[0];
        mCardRight = mTmpRect.right - mTmpOffsets[0];
        select(object);
    }

}