public class

SparseArrayObjectAdapter

extends ObjectAdapter

 java.lang.Object

androidx.leanback.widget.ObjectAdapter

↳androidx.leanback.widget.SparseArrayObjectAdapter

Gradle dependencies

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

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

Artifact androidx.leanback:leanback:1.2.0-alpha04 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.SparseArrayObjectAdapter android.support.v17.leanback.widget.SparseArrayObjectAdapter

Overview

An ObjectAdapter implemented with a . This class maintains an array of objects where each object is associated with an integer key which determines its order relative to other objects.

Summary

Fields
from ObjectAdapterNO_ID
Constructors
publicSparseArrayObjectAdapter()

Constructs an adapter.

publicSparseArrayObjectAdapter(Presenter presenter)

Constructs an adapter with the given Presenter.

publicSparseArrayObjectAdapter(PresenterSelector presenterSelector)

Constructs an adapter with the given PresenterSelector.

Methods
public voidclear()

Removes all items from this adapter, leaving it empty.

public voidclear(int key)

Clears the given key and associated item from the adapter.

public abstract java.lang.Objectget(int position)

Returns the item for the given position.

public intindexOf(int key)

Returns the index for the given key in the adapter.

public intindexOf(java.lang.Object item)

Returns the index for the given item in the adapter.

public booleanisImmediateNotifySupported()

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

public java.lang.Objectlookup(int key)

Returns the object for the given key, or null if no mapping for that key exists.

public voidnotifyArrayItemRangeChanged(int positionStart, int itemCount)

Notify that the content of a range of items changed.

public voidset(int key, java.lang.Object item)

Sets the item for the given key.

public abstract intsize()

Returns the number of items in the adapter.

from ObjectAdaptergetId, getPresenter, getPresenterSelector, hasObserver, hasStableIds, notifyChanged, notifyItemMoved, notifyItemRangeChanged, notifyItemRangeChanged, notifyItemRangeInserted, notifyItemRangeRemoved, onHasStableIdsChanged, onPresenterSelectorChanged, registerObserver, setHasStableIds, setPresenterSelector, unregisterAllObservers, unregisterObserver
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SparseArrayObjectAdapter(PresenterSelector presenterSelector)

Constructs an adapter with the given PresenterSelector.

public SparseArrayObjectAdapter(Presenter presenter)

Constructs an adapter with the given Presenter.

public SparseArrayObjectAdapter()

Constructs an adapter.

Methods

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 int indexOf(java.lang.Object item)

Returns the index for the given item in the adapter.

Parameters:

item: The item to find in the array.

Returns:

Index of the item, or a negative value if not found.

public int indexOf(int key)

Returns the index for the given key in the adapter.

Parameters:

key: The key to find in the array.

Returns:

Index of the item, or a negative value if not found.

public void notifyArrayItemRangeChanged(int positionStart, int itemCount)

Notify that the content of a range of items changed. Note that this is not same as items being added or removed.

Parameters:

positionStart: The position of first item that has changed.
itemCount: The count of how many items have changed.

public void set(int key, java.lang.Object item)

Sets the item for the given key.

Parameters:

key: The key associated with the item.
item: The item associated with the key.

public void clear(int key)

Clears the given key and associated item from the adapter.

Parameters:

key: The key to be cleared.

public void clear()

Removes all items from this adapter, leaving it empty.

public java.lang.Object lookup(int key)

Returns the object for the given key, or null if no mapping for that key exists.

public boolean isImmediateNotifySupported()

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

Source

package androidx.leanback.widget;

import android.util.SparseArray;

import androidx.annotation.Nullable;

/**
 * An {@link ObjectAdapter} implemented with a {@link android.util.SparseArray}.
 * This class maintains an array of objects where each object is associated
 * with an integer key which determines its order relative to other objects.
 */
public class SparseArrayObjectAdapter extends ObjectAdapter {
    private SparseArray<Object> mItems = new SparseArray<Object>();

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

    /**
     * Constructs an adapter with the given {@link Presenter}.
     */
    public SparseArrayObjectAdapter(Presenter presenter) {
        super(presenter);
    }

    /**
     * Constructs an adapter.
     */
    public SparseArrayObjectAdapter() {
        super();
    }

    @Override
    public int size() {
        return mItems.size();
    }

    @Override
    public @Nullable Object get(int position) {
        return mItems.valueAt(position);
    }

    /**
     * Returns the index for the given item in the adapter.
     *
     * @param item  The item to find in the array.
     * @return Index of the item, or a negative value if not found.
     */
    public int indexOf(Object item) {
        return mItems.indexOfValue(item);
    }

    /**
     * Returns the index for the given key in the adapter.
     *
     * @param key The key to find in the array.
     * @return Index of the item, or a negative value if not found.
     */
    public int indexOf(int key) {
        return mItems.indexOfKey(key);
    }

    /**
     * Notify that the content of a range of items changed. Note that this is
     * not same as items being added or removed.
     *
     * @param positionStart The position of first item that has changed.
     * @param itemCount The count of how many items have changed.
     */
    public void notifyArrayItemRangeChanged(int positionStart, int itemCount) {
        notifyItemRangeChanged(positionStart, itemCount);
    }

    /**
     * Sets the item for the given key.
     *
     * @param key The key associated with the item.
     * @param item The item associated with the key.
     */
    public void set(int key, Object item) {
        int index = mItems.indexOfKey(key);
        if (index >= 0) {
            if (mItems.valueAt(index) != item) {
                mItems.setValueAt(index, item);
                notifyItemRangeChanged(index, 1);
            }
        } else {
            mItems.append(key, item);
            index = mItems.indexOfKey(key);
            notifyItemRangeInserted(index, 1);
        }
    }

    /**
     * Clears the given key and associated item from the adapter.
     *
     * @param key The key to be cleared.
     */
    public void clear(int key) {
        int index = mItems.indexOfKey(key);
        if (index >= 0) {
            mItems.removeAt(index);
            notifyItemRangeRemoved(index, 1);
        }
    }

    /**
     * Removes all items from this adapter, leaving it empty.
     */
    public void clear() {
        final int itemCount = mItems.size();
        if (itemCount == 0) {
            return;
        }
        mItems.clear();
        notifyItemRangeRemoved(0, itemCount);
    }

    /**
     * Returns the object for the given key, or null if no mapping for that key exists.
     */
    public Object lookup(int key) {
        return mItems.get(key);
    }

    @Override
    public boolean isImmediateNotifySupported() {
        return true;
    }
}