If you think the Android project ListViewAnimations listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2014 Niek Haarman/*fromwww.java2s.com*/
*
* 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 com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.OnDismissCallback;
import java.util.HashMap;
import java.util.Map;
/**
* A {@link SimpleSwipeUndoAdapter} which automatically dismisses items after a timeout.
*/publicclass TimedUndoAdapter extends SimpleSwipeUndoAdapter {
/**
* The default time in milliseconds before an item in the undo state should automatically dismiss.
*/publicstaticfinallong DEFAULT_TIMEOUT_MS = 3000;
/**
* The time in milliseconds before an item in the undo state should automatically dismiss.
* Defaults to {@value #DEFAULT_TIMEOUT_MS}.
*/privatelong mTimeoutMs = DEFAULT_TIMEOUT_MS;
/**
* A {@link android.os.Handler} to post {@link TimeoutRunnable}s to.
*/
@NonNull
privatefinal Handler mHandler = new Handler();
/**
* The {@link TimeoutRunnable}s which are posted. Keys are positions.
*///noinspection UseSparseArrays
privatefinal Map<Integer, TimeoutRunnable> mRunnables = new HashMap<>();
/**
* Creates a new {@code TimedUndoAdapterGen}, decorating given {@link android.widget.BaseAdapter}.
*
* @param undoAdapter the {@link android.widget.BaseAdapter} that is decorated. Must implement
* {@link com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.UndoAdapter}.
* @param context the {@link android.content.Context}.
* @param dismissCallback the {@link com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.OnDismissCallback} that is notified of dismissed items.
*/public <V extends BaseAdapter & UndoAdapter> TimedUndoAdapter(@NonNull final V undoAdapter, @NonNull final Context context, @NonNull final OnDismissCallback
dismissCallback) {
super(undoAdapter, context, dismissCallback);
}
/**
* Sets the time in milliseconds after which an item in the undo state should automatically dismiss.
* Defaults to {@value #DEFAULT_TIMEOUT_MS}.
*/publicvoid setTimeoutMs(finallong timeoutMs) {
mTimeoutMs = timeoutMs;
}
@Override
publicvoid onUndoShown(@NonNull final View view, finalint position) {
super.onUndoShown(view, position);
TimeoutRunnable timeoutRunnable = new TimeoutRunnable(position);
mRunnables.put(position, timeoutRunnable);
mHandler.postDelayed(timeoutRunnable, mTimeoutMs);
}
@Override
publicvoid onUndo(@NonNull final View view, finalint position) {
super.onUndo(view, position);
cancelCallback(position);
}
@Override
publicvoid onDismiss(@NonNull final View view, finalint position) {
super.onDismiss(view, position);
cancelCallback(position);
}
@Override
publicvoid dismiss(finalint position) {
super.dismiss(position);
cancelCallback(position);
}
privatevoid cancelCallback(finalint position) {
Runnable timeoutRunnable = mRunnables.get(position);
if (timeoutRunnable != null) {
mHandler.removeCallbacks(timeoutRunnable);
mRunnables.remove(position);
}
}
@Override
publicvoid onDismiss(@NonNull final ViewGroup listView, @NonNull finalint[] reverseSortedPositions) {
super.onDismiss(listView, reverseSortedPositions);
/* Adjust the pending timeout positions accordingly wrt the given dismissed positions *///noinspection UseSparseArrays
Map<Integer, TimeoutRunnable> newRunnables = new HashMap<>();
for (int position : reverseSortedPositions) {
for (int key : mRunnables.keySet()) {
TimeoutRunnable runnable = mRunnables.get(key);
if (key > position) {
key--;
runnable.setPosition(key);
newRunnables.put(key, runnable);
} elseif (key != position) {
newRunnables.put(key, runnable);
}
}
mRunnables.clear();
mRunnables.putAll(newRunnables);
newRunnables.clear();
}
}
/**
* A {@link Runnable} class which dismisses a position when executed.
*/privateclass TimeoutRunnable implements Runnable {
privateint mPosition;
TimeoutRunnable(finalint position) {
mPosition = position;
}
@Override
publicvoid run() {
dismiss(mPosition);
}
publicint getPosition() {
return mPosition;
}
publicvoid setPosition(finalint position) {
mPosition = position;
}
}
}