Updated recyclerview-selection: to version 1.1.0-alpha05

This commit is contained in:
M66B
2019-05-09 14:55:59 +02:00
parent 9ef900cfc5
commit d7ac4f486b
13 changed files with 120 additions and 107 deletions

View File

@@ -16,17 +16,18 @@
package androidx.recyclerview.selection;
import static androidx.recyclerview.selection.Shared.DEBUG;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.collection.LongSparseArray;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnChildAttachStateChangeListener;
import java.util.HashMap;
import java.util.Map;
/**
* An {@link ItemKeyProvider} that provides stable ids by way of cached
* {@link RecyclerView.Adapter} stable ids. Items enter the cache as they are laid out by
@@ -39,8 +40,10 @@ import java.util.Map;
*/
public final class StableIdKeyProvider extends ItemKeyProvider<Long> {
private static final String TAG = "StableIdKeyProvider";
private final SparseArray<Long> mPositionToKey = new SparseArray<>();
private final Map<Long, Integer> mKeyToPosition = new HashMap<Long, Integer>();
private final LongSparseArray<Integer> mKeyToPosition = new LongSparseArray<>();
private final RecyclerView mRecyclerView;
/**
@@ -76,6 +79,12 @@ public final class StableIdKeyProvider extends ItemKeyProvider<Long> {
@SuppressWarnings("WeakerAccess") /* synthetic access */
void onAttached(@NonNull View view) {
RecyclerView.ViewHolder holder = mRecyclerView.findContainingViewHolder(view);
if (holder == null) {
if (DEBUG) {
Log.w(TAG, "Unable to find ViewHolder for View. Ignoring onAttached event.");
}
return;
}
int position = holder.getAdapterPosition();
long id = holder.getItemId();
if (position != RecyclerView.NO_POSITION && id != RecyclerView.NO_ID) {
@@ -87,6 +96,12 @@ public final class StableIdKeyProvider extends ItemKeyProvider<Long> {
@SuppressWarnings("WeakerAccess") /* synthetic access */
void onDetached(@NonNull View view) {
RecyclerView.ViewHolder holder = mRecyclerView.findContainingViewHolder(view);
if (holder == null) {
if (DEBUG) {
Log.w(TAG, "Unable to find ViewHolder for View. Ignoring onDetached event.");
}
return;
}
int position = holder.getAdapterPosition();
long id = holder.getItemId();
if (position != RecyclerView.NO_POSITION && id != RecyclerView.NO_ID) {
@@ -102,9 +117,6 @@ public final class StableIdKeyProvider extends ItemKeyProvider<Long> {
@Override
public int getPosition(@NonNull Long key) {
if (mKeyToPosition.containsKey(key)) {
return mKeyToPosition.get(key);
}
return RecyclerView.NO_POSITION;
return mKeyToPosition.get(key, RecyclerView.NO_POSITION);
}
}