public abstract class

ItemKeyProvider<K>

extends java.lang.Object

 java.lang.Object

↳androidx.recyclerview.selection.ItemKeyProvider<K>

Subclasses:

StableIdKeyProvider

Gradle dependencies

compile group: 'androidx.recyclerview', name: 'recyclerview-selection', version: '1.2.0-alpha01'

  • groupId: androidx.recyclerview
  • artifactId: recyclerview-selection
  • version: 1.2.0-alpha01

Artifact androidx.recyclerview:recyclerview-selection:1.2.0-alpha01 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.recyclerview:recyclerview-selection com.android.support:recyclerview-selection

Overview

Provides selection library access to stable selection keys identifying items presented by a RecyclerView instance.

Summary

Fields
public static final intSCOPE_CACHED

Provides access to cached data based for items that were recently bound in the view.

public static final intSCOPE_MAPPED

Provides access to all data, regardless of whether it is bound to a view or not.

Constructors
protectedItemKeyProvider(int scope)

Creates a new provider with the given scope.

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

public abstract intgetPosition(java.lang.Object key)

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

Fields

public static final int SCOPE_MAPPED

Provides access to all data, regardless of whether it is bound to a view or not. Key providers with this access type enjoy support for enhanced features like: SHIFT+click range selection, and band selection.

public static final int SCOPE_CACHED

Provides access to cached data based for items that were recently bound in the view. Employing this provider will result in a reduced feature-set, as some features like SHIFT+click range selection and band selection are dependent on mapped access.

Constructors

protected ItemKeyProvider(int scope)

Creates a new provider with the given scope.

Parameters:

scope: Scope can't be changed at runtime.

Methods

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

Returns:

The selection key at the given adapter position, or null.

public abstract int getPosition(java.lang.Object key)

Returns:

the position corresponding to the selection key, or RecyclerView.NO_POSITION if the key is unrecognized.

Source

/*
 * Copyright 2017 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.recyclerview.selection;

import static androidx.core.util.Preconditions.checkArgument;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Provides selection library access to stable selection keys identifying items
 * presented by a {@link RecyclerView RecyclerView} instance.
 *
 * @param <K> Selection key type. @see {@link StorageStrategy} for supported types.
 */
public abstract class ItemKeyProvider<K> {

    /**
     * Provides access to all data, regardless of whether it is bound to a view or not.
     * Key providers with this access type enjoy support for enhanced features like:
     * SHIFT+click range selection, and band selection.
     */
    public static final int SCOPE_MAPPED = 0;

    /**
     * Provides access to cached data based for items that were recently bound in the view.
     * Employing this provider will result in a reduced feature-set, as some
     * features like SHIFT+click range selection and band selection are dependent
     * on mapped access.
     */
    public static final int SCOPE_CACHED = 1;

    @IntDef({
            SCOPE_MAPPED,
            SCOPE_CACHED
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface Scope {}

    private final @Scope int mScope;

    /**
     * Creates a new provider with the given scope.
     *
     * @param scope Scope can't be changed at runtime.
     */
    protected ItemKeyProvider(@Scope int scope) {
        checkArgument(scope == SCOPE_MAPPED || scope == SCOPE_CACHED);

        mScope = scope;
    }

    final boolean hasAccess(@Scope int scope) {
        return scope == mScope;
    }

    /**
     * @return The selection key at the given adapter position, or null.
     */
    public abstract @Nullable K getKey(int position);

    /**
     * @return the position corresponding to the selection key, or RecyclerView.NO_POSITION
     * if the key is unrecognized.
     */
    public abstract int getPosition(@NonNull K key);
}