Android Open Source - ListView-Swipe-to-Delete Item Exp List Manager






From Project

Back to project page ListView-Swipe-to-Delete.

License

The source code is released under:

Apache License

If you think the Android project ListView-Swipe-to-Delete 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 (C) 2013 Steelkiwi Development, Julia Zudikova
 * //from  w w  w . j  a  v  a 2 s  .co  m
 * 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.skd.swipetodelete.explist;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Vibrator;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.skd.swipetodelete.shake.ShakeDetectActivity;
import com.skd.swipetodelete.shake.ShakeDetectActivityListener;

/*
 * Class responsible for establishing expandable list view gesture listener and device shake detector.
 * Shake detector is used to remove a group of marked expandable list view items. 
 */

public class ItemExpListManager {
  private Context ctx;
  private ItemBaseExpListHandler listHandler;
  private GestureDetector swipeDetector;
  private ShakeDetectActivity shakeDetector;
  
  public ItemExpListManager(Context ctx, ItemBaseExpListHandler handler, boolean useShake) {
    this.ctx = ctx;
    this.listHandler = handler;
    
    swipeDetector = new GestureDetector(ctx, new ItemExpListGestureDetector(ctx.getResources().getDisplayMetrics(), listHandler));
    
    View.OnTouchListener gestureListener = new View.OnTouchListener() {
      public boolean onTouch(View v, MotionEvent event) {
        return swipeDetector.onTouchEvent(event);
      }
    };
    if ((listHandler != null) && (listHandler.getListView() != null)) {
      listHandler.getListView().setOnTouchListener(gestureListener);
    }
    
    if (useShake) {
      shakeDetector = new ShakeDetectActivity(ctx);
      shakeDetector.addListener(new ShakeDetectActivityListener() {
        @Override
        public void shakeDetected() {
          if ((listHandler != null) && (!listHandler.hasMarkedToRemoveItems())) {
            return;
          }
          showRemoveItemsConfirmDialog();
        }
      });
    }  
  }
  
  public void resume() {
    if (shakeDetector != null) {
      shakeDetector.onResume();
    }
  }

  public void pause() {
    if (shakeDetector != null) {
      shakeDetector.onPause();
    }
  }
  
  public void destroy() {
    ctx = null;
    listHandler = null;
    swipeDetector = null;
    shakeDetector = null;
  }
  
  private void showRemoveItemsConfirmDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setMessage("Remove all selected items?")
        .setCancelable(false)
        .setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                if (listHandler != null) { 
                  listHandler.removeAllItems();
                }
              }
            })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
          }
        });
    AlertDialog alert = builder.create();
    alert.show();
    Vibrator v = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);
    if (v != null) { v.vibrate(500); }
  }
}




Java Source Code List

com.skd.swipetodelete.ItemBase.java
com.skd.swipetodelete.Item.java
com.skd.swipetodelete.MainActivity.java
com.skd.swipetodelete.explist.ItemBaseExpListAdapter.java
com.skd.swipetodelete.explist.ItemBaseExpListHandler.java
com.skd.swipetodelete.explist.ItemExpListAdapter.java
com.skd.swipetodelete.explist.ItemExpListGestureDetector.java
com.skd.swipetodelete.explist.ItemExpListManager.java
com.skd.swipetodelete.explist.MainExpListActivity.java
com.skd.swipetodelete.list.ItemBaseListAdapter.java
com.skd.swipetodelete.list.ItemBaseListHandler.java
com.skd.swipetodelete.list.ItemListAdapter.java
com.skd.swipetodelete.list.ItemListGestureDetector.java
com.skd.swipetodelete.list.ItemListManager.java
com.skd.swipetodelete.list.MainListActivity.java
com.skd.swipetodelete.menu.MenuItemBuilder.java
com.skd.swipetodelete.menu.MenuItemDesc.java
com.skd.swipetodelete.shake.ShakeDetectActivityListener.java
com.skd.swipetodelete.shake.ShakeDetectActivity.java
com.skd.swipetodelete.utils.DimenUtils.java