public class

LeanbackViewPager

extends ViewPager

 java.lang.Object

↳ViewGroup

androidx.viewpager.widget.ViewPager

↳androidx.leanback.tab.LeanbackViewPager

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback-tab', version: '1.1.0-beta01'

  • groupId: androidx.leanback
  • artifactId: leanback-tab
  • version: 1.1.0-beta01

Artifact androidx.leanback:leanback-tab:1.1.0-beta01 it located at Google repository (https://maven.google.com/)

Overview

A viewpager with touch and key event handling disabled by default.

Key events handling is disabled by default as with the behaviour of viewpager the fragments can change when DPAD keys are pressed and focus is on the content inside the ViewPager. This is not desirable for a top navigation bar. The fragments should preferably change only when the focused tab changes.

Summary

Fields
from ViewPagermLeftEdge, mRightEdge, SCROLL_STATE_DRAGGING, SCROLL_STATE_IDLE, SCROLL_STATE_SETTLING
Constructors
publicLeanbackViewPager(Context context)

Constructs LeanbackViewPager

publicLeanbackViewPager(Context context, AttributeSet attrs)

Constructs LeanbackViewPager

Methods
public booleanexecuteKeyEvent(KeyEvent event)

You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.

public booleanonInterceptTouchEvent(MotionEvent event)

public booleanonTouchEvent(MotionEvent event)

public voidsetKeyEventsEnabled(boolean enableKeyEvent)

Setter for enabling/disabling key events

public voidsetTouchEnabled(boolean enableTouch)

Setter for enabling/disabling touch events

from ViewPageraddFocusables, addOnAdapterChangeListener, addOnPageChangeListener, addTouchables, addView, arrowScroll, beginFakeDrag, canScroll, canScrollHorizontally, checkLayoutParams, clearOnPageChangeListeners, computeScroll, dispatchKeyEvent, dispatchPopulateAccessibilityEvent, draw, drawableStateChanged, endFakeDrag, fakeDragBy, generateDefaultLayoutParams, generateLayoutParams, generateLayoutParams, getAdapter, getChildDrawingOrder, getCurrentItem, getOffscreenPageLimit, getPageMargin, isDragInGutterEnabled, isFakeDragging, onAttachedToWindow, onDetachedFromWindow, onDraw, onLayout, onMeasure, onPageScrolled, onRequestFocusInDescendants, onRestoreInstanceState, onSaveInstanceState, onSizeChanged, removeOnAdapterChangeListener, removeOnPageChangeListener, removeView, setAdapter, setCurrentItem, setCurrentItem, setDragInGutterEnabled, setOffscreenPageLimit, setOnPageChangeListener, setPageMargin, setPageMarginDrawable, setPageTransformer, setPageTransformer, verifyDrawable
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public LeanbackViewPager(Context context)

Constructs LeanbackViewPager

Parameters:

context:

public LeanbackViewPager(Context context, AttributeSet attrs)

Constructs LeanbackViewPager

Parameters:

context:
attrs:

Methods

public boolean onTouchEvent(MotionEvent event)

public boolean onInterceptTouchEvent(MotionEvent event)

public boolean executeKeyEvent(KeyEvent event)

You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.

Parameters:

event: The key event to execute.

Returns:

Return true if the event was handled, else false.

public void setTouchEnabled(boolean enableTouch)

Setter for enabling/disabling touch events

Parameters:

enableTouch:

public void setKeyEventsEnabled(boolean enableKeyEvent)

Setter for enabling/disabling key events

Parameters:

enableKeyEvent:

Source

/*
 * Copyright 2020 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.tab;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

/**
 * A viewpager with touch and key event handling disabled by default.
 *
 * <p>Key events handling is disabled by default as with the behaviour of viewpager the fragments
 * can change when DPAD keys are pressed and focus is on the content inside the {@link ViewPager}.
 * This is not desirable for a top navigation bar. The fragments should preferably change only
 * when the focused tab changes.
 */
public class LeanbackViewPager extends ViewPager {

    private boolean mTouchEnabled = false;
    private boolean mEnableKeyEvent = false;

    /**
     * Constructs LeanbackViewPager
     * @param context
     */
    public LeanbackViewPager(@NonNull Context context) {
        super(context);
    }

    /**
     * Constructs LeanbackViewPager
     * @param context
     * @param attrs
     */
    public LeanbackViewPager(@NonNull Context context,
            @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        return mTouchEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(@NonNull MotionEvent event) {
        return mTouchEnabled && super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean executeKeyEvent(@NonNull KeyEvent event) {
        return mEnableKeyEvent && super.executeKeyEvent(event);
    }

    /**
     * Setter for enabling/disabling touch events
     * @param enableTouch
     */
    public void setTouchEnabled(boolean enableTouch) {
        mTouchEnabled = enableTouch;
    }

    /**
     * Setter for enabling/disabling key events
     * @param enableKeyEvent
     */
    public void setKeyEventsEnabled(boolean enableKeyEvent) {
        mEnableKeyEvent = enableKeyEvent;
    }
}