public abstract class

ObjectAdapter

extends java.lang.Object

 java.lang.Object

↳androidx.leanback.widget.ObjectAdapter

Subclasses:

ArrayObjectAdapter, CursorObjectAdapter, SparseArrayObjectAdapter

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.ObjectAdapter android.support.v17.leanback.widget.ObjectAdapter

Overview

Base class adapter to be used in leanback activities. Provides access to a data model and is decoupled from the presentation of the items via PresenterSelector.

Summary

Fields
public static final intNO_ID

Indicates that an id has not been set.

Constructors
publicObjectAdapter()

Constructs an adapter.

publicObjectAdapter(Presenter presenter)

Constructs an adapter that uses the given Presenter for all items.

publicObjectAdapter(PresenterSelector presenterSelector)

Constructs an adapter with the given PresenterSelector.

Methods
public abstract java.lang.Objectget(int position)

Returns the item for the given position.

public longgetId(int position)

Returns the id for the given position.

public final PresentergetPresenter(java.lang.Object item)

Returns the Presenter for the given item from the adapter.

public final PresenterSelectorgetPresenterSelector()

Returns the presenter selector for this ObjectAdapter.

public final booleanhasObserver()

public final booleanhasStableIds()

Returns true if the item ids are stable across changes to the underlying data.

public booleanisImmediateNotifySupported()

Returns true if the adapter pairs each underlying data change with a call to notify and false otherwise.

protected final voidnotifyChanged()

Notifies UI that the underlying data has changed.

protected final voidnotifyItemMoved(int fromPosition, int toPosition)

Notifies UI that item at fromPosition has been moved to toPosition.

public final voidnotifyItemRangeChanged(int positionStart, int itemCount)

Notifies UI that some items has changed.

public final voidnotifyItemRangeChanged(int positionStart, int itemCount, java.lang.Object payload)

Notifies UI that some items has changed.

protected final voidnotifyItemRangeInserted(int positionStart, int itemCount)

Notifies UI that new items has been inserted.

protected final voidnotifyItemRangeRemoved(int positionStart, int itemCount)

Notifies UI that some items that has been removed.

protected voidonHasStableIdsChanged()

Called when ObjectAdapter.setHasStableIds(boolean) is called and the status of stable ids has changed.

protected voidonPresenterSelectorChanged()

Called when ObjectAdapter.setPresenterSelector(PresenterSelector) is called and the PresenterSelector differs from the previous one.

public final voidregisterObserver(ObjectAdapter.DataObserver observer)

Registers a DataObserver for data change notifications.

public final voidsetHasStableIds(boolean hasStableIds)

Sets whether the item ids are stable across changes to the underlying data.

public final voidsetPresenterSelector(PresenterSelector presenterSelector)

Sets the presenter selector.

public abstract intsize()

Returns the number of items in the adapter.

public final voidunregisterAllObservers()

Unregisters all DataObservers for this ObjectAdapter.

public final voidunregisterObserver(ObjectAdapter.DataObserver observer)

Unregisters a DataObserver for data change notifications.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int NO_ID

Indicates that an id has not been set.

Constructors

public ObjectAdapter(PresenterSelector presenterSelector)

Constructs an adapter with the given PresenterSelector.

public ObjectAdapter(Presenter presenter)

Constructs an adapter that uses the given Presenter for all items.

public ObjectAdapter()

Constructs an adapter.

Methods

public final void setPresenterSelector(PresenterSelector presenterSelector)

Sets the presenter selector. May not be null.

protected void onPresenterSelectorChanged()

Called when ObjectAdapter.setPresenterSelector(PresenterSelector) is called and the PresenterSelector differs from the previous one.

public final PresenterSelector getPresenterSelector()

Returns the presenter selector for this ObjectAdapter.

public final void registerObserver(ObjectAdapter.DataObserver observer)

Registers a DataObserver for data change notifications.

public final void unregisterObserver(ObjectAdapter.DataObserver observer)

Unregisters a DataObserver for data change notifications.

public final boolean hasObserver()

public final void unregisterAllObservers()

Unregisters all DataObservers for this ObjectAdapter.

public final void notifyItemRangeChanged(int positionStart, int itemCount)

Notifies UI that some items has changed.

Parameters:

positionStart: Starting position of the changed items.
itemCount: Total number of items that changed.

public final void notifyItemRangeChanged(int positionStart, int itemCount, java.lang.Object payload)

Notifies UI that some items has changed.

Parameters:

positionStart: Starting position of the changed items.
itemCount: Total number of items that changed.
payload: Optional parameter, use null to identify a "full" update.

protected final void notifyItemRangeInserted(int positionStart, int itemCount)

Notifies UI that new items has been inserted.

Parameters:

positionStart: Position where new items has been inserted.
itemCount: Count of the new items has been inserted.

protected final void notifyItemRangeRemoved(int positionStart, int itemCount)

Notifies UI that some items that has been removed.

Parameters:

positionStart: Starting position of the removed items.
itemCount: Total number of items that has been removed.

protected final void notifyItemMoved(int fromPosition, int toPosition)

Notifies UI that item at fromPosition has been moved to toPosition.

Parameters:

fromPosition: Previous position of the item.
toPosition: New position of the item.

protected final void notifyChanged()

Notifies UI that the underlying data has changed.

public final boolean hasStableIds()

Returns true if the item ids are stable across changes to the underlying data. When this is true, clients of the ObjectAdapter can use ObjectAdapter.getId(int) to correlate Objects across changes.

public final void setHasStableIds(boolean hasStableIds)

Sets whether the item ids are stable across changes to the underlying data.

protected void onHasStableIdsChanged()

Called when ObjectAdapter.setHasStableIds(boolean) is called and the status of stable ids has changed.

public final Presenter getPresenter(java.lang.Object item)

Returns the Presenter for the given item from the adapter.

public abstract int size()

Returns the number of items in the adapter.

public abstract java.lang.Object get(int position)

Returns the item for the given position.

public long getId(int position)

Returns the id for the given position.

public boolean isImmediateNotifySupported()

Returns true if the adapter pairs each underlying data change with a call to notify and false otherwise.

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 static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;

import android.database.Observable;

import androidx.annotation.RestrictTo;

/**
 * Base class adapter to be used in leanback activities.  Provides access to a data model and is
 * decoupled from the presentation of the items via {@link PresenterSelector}.
 */
public abstract class ObjectAdapter {

    /** Indicates that an id has not been set. */
    public static final int NO_ID = -1;

    /**
     * A DataObserver can be notified when an ObjectAdapter's underlying data
     * changes. Separate methods provide notifications about different types of
     * changes.
     */
    public static abstract class DataObserver {
        /**
         * Called whenever the ObjectAdapter's data has changed in some manner
         * outside of the set of changes covered by the other range-based change
         * notification methods.
         */
        public void onChanged() {
        }

        /**
         * Called when a range of items in the ObjectAdapter has changed. The
         * basic ordering and structure of the ObjectAdapter has not changed.
         *
         * @param positionStart The position of the first item that changed.
         * @param itemCount     The number of items changed.
         */
        public void onItemRangeChanged(int positionStart, int itemCount) {
            onChanged();
        }

        /**
         * Called when a range of items in the ObjectAdapter has changed. The
         * basic ordering and structure of the ObjectAdapter has not changed.
         *
         * @param positionStart The position of the first item that changed.
         * @param itemCount     The number of items changed.
         * @param payload       Optional parameter, use null to identify a "full" update.
         */
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            onChanged();
        }

        /**
         * Called when a range of items is inserted into the ObjectAdapter.
         *
         * @param positionStart The position of the first inserted item.
         * @param itemCount     The number of items inserted.
         */
        public void onItemRangeInserted(int positionStart, int itemCount) {
            onChanged();
        }

        /**
         * Called when an item is moved from one position to another position
         *
         * @param fromPosition Previous position of the item.
         * @param toPosition   New position of the item.
         */
        public void onItemMoved(int fromPosition, int toPosition) {
            onChanged();
        }

        /**
         * Called when a range of items is removed from the ObjectAdapter.
         *
         * @param positionStart The position of the first removed item.
         * @param itemCount     The number of items removed.
         */
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            onChanged();
        }
    }

    private static final class DataObservable extends Observable<DataObserver> {

        DataObservable() {
        }

        public void notifyChanged() {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onChanged();
            }
        }

        public void notifyItemRangeChanged(int positionStart, int itemCount) {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemRangeChanged(positionStart, itemCount);
            }
        }

        public void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemRangeChanged(positionStart, itemCount, payload);
            }
        }

        public void notifyItemRangeInserted(int positionStart, int itemCount) {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemRangeInserted(positionStart, itemCount);
            }
        }

        public void notifyItemRangeRemoved(int positionStart, int itemCount) {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemRangeRemoved(positionStart, itemCount);
            }
        }

        public void notifyItemMoved(int positionStart, int toPosition) {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onItemMoved(positionStart, toPosition);
            }
        }

        boolean hasObserver() {
            return mObservers.size() > 0;
        }
    }

    private final DataObservable mObservable = new DataObservable();
    private boolean mHasStableIds;
    private PresenterSelector mPresenterSelector;

    /**
     * Constructs an adapter with the given {@link PresenterSelector}.
     */
    public ObjectAdapter(PresenterSelector presenterSelector) {
        setPresenterSelector(presenterSelector);
    }

    /**
     * Constructs an adapter that uses the given {@link Presenter} for all items.
     */
    public ObjectAdapter(Presenter presenter) {
        setPresenterSelector(new SinglePresenterSelector(presenter));
    }

    /**
     * Constructs an adapter.
     */
    public ObjectAdapter() {
    }

    /**
     * Sets the presenter selector.  May not be null.
     */
    public final void setPresenterSelector(PresenterSelector presenterSelector) {
        if (presenterSelector == null) {
            throw new IllegalArgumentException("Presenter selector must not be null");
        }
        final boolean update = (mPresenterSelector != null);
        final boolean selectorChanged = update && mPresenterSelector != presenterSelector;

        mPresenterSelector = presenterSelector;

        if (selectorChanged) {
            onPresenterSelectorChanged();
        }
        if (update) {
            notifyChanged();
        }
    }

    /**
     * Called when {@link #setPresenterSelector(PresenterSelector)} is called
     * and the PresenterSelector differs from the previous one.
     */
    protected void onPresenterSelectorChanged() {
    }

    /**
     * Returns the presenter selector for this ObjectAdapter.
     */
    public final PresenterSelector getPresenterSelector() {
        return mPresenterSelector;
    }

    /**
     * Registers a DataObserver for data change notifications.
     */
    public final void registerObserver(DataObserver observer) {
        mObservable.registerObserver(observer);
    }

    /**
     * Unregisters a DataObserver for data change notifications.
     */
    public final void unregisterObserver(DataObserver observer) {
        mObservable.unregisterObserver(observer);
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY_GROUP_PREFIX)
    public final boolean hasObserver() {
        return mObservable.hasObserver();
    }

    /**
     * Unregisters all DataObservers for this ObjectAdapter.
     */
    public final void unregisterAllObservers() {
        mObservable.unregisterAll();
    }

    /**
     * Notifies UI that some items has changed.
     *
     * @param positionStart Starting position of the changed items.
     * @param itemCount     Total number of items that changed.
     */
    public final void notifyItemRangeChanged(int positionStart, int itemCount) {
        mObservable.notifyItemRangeChanged(positionStart, itemCount);
    }

    /**
     * Notifies UI that some items has changed.
     *
     * @param positionStart Starting position of the changed items.
     * @param itemCount     Total number of items that changed.
     * @param payload       Optional parameter, use null to identify a "full" update.
     */
    public final void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) {
        mObservable.notifyItemRangeChanged(positionStart, itemCount, payload);
    }

    /**
     * Notifies UI that new items has been inserted.
     *
     * @param positionStart Position where new items has been inserted.
     * @param itemCount     Count of the new items has been inserted.
     */
    final protected void notifyItemRangeInserted(int positionStart, int itemCount) {
        mObservable.notifyItemRangeInserted(positionStart, itemCount);
    }

    /**
     * Notifies UI that some items that has been removed.
     *
     * @param positionStart Starting position of the removed items.
     * @param itemCount     Total number of items that has been removed.
     */
    final protected void notifyItemRangeRemoved(int positionStart, int itemCount) {
        mObservable.notifyItemRangeRemoved(positionStart, itemCount);
    }

    /**
     * Notifies UI that item at fromPosition has been moved to toPosition.
     *
     * @param fromPosition Previous position of the item.
     * @param toPosition   New position of the item.
     */
    protected final void notifyItemMoved(int fromPosition, int toPosition) {
        mObservable.notifyItemMoved(fromPosition, toPosition);
    }

    /**
     * Notifies UI that the underlying data has changed.
     */
    final protected void notifyChanged() {
        mObservable.notifyChanged();
    }

    /**
     * Returns true if the item ids are stable across changes to the
     * underlying data.  When this is true, clients of the ObjectAdapter can use
     * {@link #getId(int)} to correlate Objects across changes.
     */
    public final boolean hasStableIds() {
        return mHasStableIds;
    }

    /**
     * Sets whether the item ids are stable across changes to the underlying
     * data.
     */
    public final void setHasStableIds(boolean hasStableIds) {
        boolean changed = mHasStableIds != hasStableIds;
        mHasStableIds = hasStableIds;

        if (changed) {
            onHasStableIdsChanged();
        }
    }

    /**
     * Called when {@link #setHasStableIds(boolean)} is called and the status
     * of stable ids has changed.
     */
    protected void onHasStableIdsChanged() {
    }

    /**
     * Returns the {@link Presenter} for the given item from the adapter.
     */
    public final Presenter getPresenter(Object item) {
        if (mPresenterSelector == null) {
            throw new IllegalStateException("Presenter selector must not be null");
        }
        return mPresenterSelector.getPresenter(item);
    }

    /**
     * Returns the number of items in the adapter.
     */
    public abstract int size();

    /**
     * Returns the item for the given position.
     */
    public abstract Object get(int position);

    /**
     * Returns the id for the given position.
     */
    public long getId(int position) {
        return NO_ID;
    }

    /**
     * Returns true if the adapter pairs each underlying data change with a call to notify and
     * false otherwise.
     */
    public boolean isImmediateNotifySupported() {
        return false;
    }
}