Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback aboutToDismiss in SwipeDismissListViewTouchListener #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/com/example/android/swipedismiss/MainActivity.java
Expand Up @@ -16,6 +16,9 @@

package com.example.android.swipedismiss;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
Expand All @@ -25,9 +28,6 @@
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends ListActivity {
ArrayAdapter<String> mAdapter;

Expand Down Expand Up @@ -59,6 +59,17 @@ public void onCreate(Bundle savedInstanceState) {
public boolean canDismiss(int position) {
return true;
}

@Override
public void aboutToDismiss(int position) {
/* Notified before the dismiss actually takes place which alters the positions/data
*
* With more complex data in your cells, this provides the ability to update the
* object based on this position before the list change
*
* if needed adapter.remove(adapter.getItem(position));
*/
}

@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
Expand Down
Expand Up @@ -16,6 +16,10 @@

package com.example.android.swipedismiss;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
Expand All @@ -30,10 +34,6 @@
import android.widget.AbsListView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* A {@link View.OnTouchListener} that makes the list items in a {@link ListView}
* dismissable. {@link ListView} is given special treatment because by default it handles touches
Expand All @@ -50,17 +50,31 @@
* <p>Example usage:</p>
*
* <pre>
* SwipeDismissListViewTouchListener touchListener =
* new SwipeDismissListViewTouchListener(
* listView,
* new SwipeDismissListViewTouchListener.OnDismissCallback() {
* public void onDismiss(ListView listView, int[] reverseSortedPositions) {
* for (int position : reverseSortedPositions) {
* adapter.remove(adapter.getItem(position));
* }
* adapter.notifyDataSetChanged();
* }
* });
*
* SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(listView, new DismissCallbacks() {

@Override
public boolean canDismiss(int position) {
// all cells in the listView can be swiped, make conditional if you like
return true;
}

@Override
public void aboutToDismiss(int position) {
// notified before the dismiss actually takes place which alters the positions/data
// provides ability to update the object based on this position before the list change

// if needed adapter.remove(adapter.getItem(position));

}

@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
// if needed, adapter.notifyDataSetChanged();
}

});
*
* listView.setOnTouchListener(touchListener);
* listView.setOnScrollListener(touchListener.makeScrollListener());
* </pre>
Expand Down Expand Up @@ -106,6 +120,11 @@ public interface DismissCallbacks {
* Called to determine whether the given position can be dismissed.
*/
boolean canDismiss(int position);

/**
* Called right before the dismiss behavior happens
*/
void aboutToDismiss(int position);

/**
* Called when the user has indicated they she would like to dismiss one or more list item
Expand Down Expand Up @@ -341,6 +360,7 @@ public int compareTo(PendingDismissData other) {
}

private void performDismiss(final View dismissView, final int dismissPosition) {
mCallbacks.aboutToDismiss(dismissPosition);
// Animate the dismissed list item to zero-height and fire the dismiss callback when
// all dismissed list item animations have completed. This triggers layout on each animation
// frame; in the future we may want to do something smarter and more performant.
Expand Down