Ejemplo n.º 1
0
  /** @see android.widget.ListView#onInterceptTouchEvent(android.view.MotionEvent) */
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);
    final float x = ev.getX();
    final float y = ev.getY();

    if (isEnabled() && touchListener.isSwipeEnabled()) {

      if (touchState == TOUCH_STATE_SCROLLING_X) {
        return touchListener.onTouch(this, ev);
      }

      switch (action) {
        case MotionEvent.ACTION_MOVE:
          checkInMoving(x, y);
          return touchState == TOUCH_STATE_SCROLLING_Y;
        case MotionEvent.ACTION_DOWN:
          touchListener.onTouch(this, ev);
          touchState = TOUCH_STATE_REST;
          lastMotionX = x;
          lastMotionY = y;
          return false;
        case MotionEvent.ACTION_CANCEL:
          touchState = TOUCH_STATE_REST;
          break;
        case MotionEvent.ACTION_UP:
          touchListener.onTouch(this, ev);
          return touchState == TOUCH_STATE_SCROLLING_Y;
        default:
          break;
      }
    }

    return super.onInterceptTouchEvent(ev);
  }
Ejemplo n.º 2
0
 /**
  * Dismiss item
  *
  * @param position Position that you want open
  */
 public void dismiss(int position) {
   int height = touchListener.dismiss(position);
   if (height > 0) {
     touchListener.handlerPendingDismisses(height);
   } else {
     int[] dismissPositions = new int[1];
     dismissPositions[0] = position;
     onDismiss(dismissPositions);
     touchListener.resetPendingDismisses();
   }
 }
Ejemplo n.º 3
0
  /**
   * Recycle cell. This method should be called from getView in Adapter when use SWIPE_ACTION_CHOICE
   *
   * @param convertView parent view
   * @param position position in list
   */
  public void recycle(View convertView, int position) {
    touchListener.reloadChoiceStateInView(convertView.findViewById(swipeFrontView), position);
    touchListener.reloadSwipeStateInView(convertView.findViewById(swipeFrontView), position);

    // Clean pressed state (if dismiss is fire from a cell, to this cell, with a press drawable, in
    // a swipelistview
    // when this cell will be recycle it will still have his pressed state. This ensure the pressed
    // state is
    // cleaned.
    for (int j = 0; j < ((ViewGroup) convertView).getChildCount(); ++j) {
      View nextChild = ((ViewGroup) convertView).getChildAt(j);
      nextChild.setPressed(false);
    }
  }
Ejemplo n.º 4
0
  // 左边滑出多长,单位dip
  public void setOffsetLeft(float leftLengthDip) {
    float screenWidthPix = getContext().getResources().getDisplayMetrics().widthPixels;
    final float scale = getContext().getResources().getDisplayMetrics().density;
    float leftPix = (leftLengthDip * scale + 0.5f);

    touchListener.setLeftOffset(screenWidthPix - leftPix);
  }
 /** Dismiss items selected */
 public void dismissSelected() {
   List<Integer> list = touchListener.getPositionsSelected();
   int[] dismissPositions = new int[list.size()];
   int height = 0;
   for (int i = 0; i < list.size(); i++) {
     int position = list.get(i);
     dismissPositions[i] = position;
     int auxHeight = touchListener.dismiss(position);
     if (auxHeight > 0) {
       height = auxHeight;
     }
   }
   if (height > 0) {
     touchListener.handlerPendingDismisses(height);
   } else {
     onDismiss(dismissPositions);
     touchListener.resetPendingDismisses();
   }
 }
Ejemplo n.º 6
0
  /**
   * Init ListView
   *
   * @param attrs AttributeSet
   */
  private void init(AttributeSet attrs) {

    float swipeOffsetLeft = 0;
    int swipeDrawableChecked = 0;
    int swipeDrawableUnchecked = 0;

    if (attrs != null) {
      TypedArray styled = getContext().obtainStyledAttributes(attrs, R.styleable.SwipeListView);

      swipeFrontView = styled.getResourceId(R.styleable.SwipeListView_swipeFrontView, 0);
      swipeBackView = styled.getResourceId(R.styleable.SwipeListView_swipeBackView, 0);
      styled.recycle();
    }

    if (swipeFrontView == 0 || swipeBackView == 0) {
      swipeFrontView =
          getContext()
              .getResources()
              .getIdentifier(SWIPE_DEFAULT_FRONT_VIEW, "id", getContext().getPackageName());
      swipeBackView =
          getContext()
              .getResources()
              .getIdentifier(SWIPE_DEFAULT_BACK_VIEW, "id", getContext().getPackageName());

      if (swipeFrontView == 0 || swipeBackView == 0) {
        throw new RuntimeException(
            String.format(
                "You forgot the attributes swipeFrontView or swipeBackView. You can add this attributes or use '%s' and '%s' identifiers",
                SWIPE_DEFAULT_FRONT_VIEW, SWIPE_DEFAULT_BACK_VIEW));
      }
    }

    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    touchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    touchListener = new SwipeListViewTouchListener(this, swipeFrontView, swipeBackView);
    touchListener.setLeftOffset(swipeOffsetLeft);
    touchListener.setSwipeDrawableChecked(swipeDrawableChecked);
    touchListener.setSwipeDrawableUnchecked(swipeDrawableUnchecked);
    setOnTouchListener(touchListener);
    setOnScrollListener(touchListener.makeScrollListener());
  }
Ejemplo n.º 7
0
 /** @see android.widget.ListView#setAdapter(android.widget.ListAdapter) */
 @Override
 public void setAdapter(ListAdapter adapter) {
   super.setAdapter(adapter);
   touchListener.resetItems();
   adapter.registerDataSetObserver(
       new DataSetObserver() {
         @Override
         public void onChanged() {
           super.onChanged();
           onListChanged();
           touchListener.resetItems();
         }
       });
 }
Ejemplo n.º 8
0
 /**
  * Return action on right
  *
  * @return Action
  */
 public int getSwipeActionRight() {
   return touchListener.getSwipeActionRight();
 }
Ejemplo n.º 9
0
 /**
  * Set action on left
  *
  * @param swipeActionLeft Action
  */
 public void setSwipeActionLeft(int swipeActionLeft) {
   touchListener.setSwipeActionLeft(swipeActionLeft);
 }
Ejemplo n.º 10
0
 /**
  * Set swipe mode
  *
  * @param swipeMode
  */
 public void setSwipeMode(int swipeMode) {
   touchListener.setSwipeMode(swipeMode);
 }
Ejemplo n.º 11
0
 /**
  * Sets if the user can open an item with long pressing on cell
  *
  * @param swipeOpenOnLongPress
  */
 public void setSwipeOpenOnLongPress(boolean swipeOpenOnLongPress) {
   touchListener.setSwipeOpenOnLongPress(swipeOpenOnLongPress);
 }
Ejemplo n.º 12
0
 /**
  * Set if all items opened will be closed when the user moves the ListView
  *
  * @param swipeCloseAllItemsWhenMoveList
  */
 public void setSwipeCloseAllItemsWhenMoveList(boolean swipeCloseAllItemsWhenMoveList) {
   touchListener.setSwipeClosesAllItemsWhenListMoves(swipeCloseAllItemsWhenMoveList);
 }
Ejemplo n.º 13
0
 /**
  * Set offset on left
  *
  * @param offsetLeft Offset
  */
 public void setOffsetLeft(float offsetLeft) {
   touchListener.setLeftOffset(offsetLeft);
 }
Ejemplo n.º 14
0
 /**
  * Get if item is selected
  *
  * @param position position in list
  * @return
  */
 public boolean isChecked(int position) {
   return touchListener.isChecked(position);
 }
Ejemplo n.º 15
0
 /**
  * Close ListView's item
  *
  * @param position Position that you want open
  */
 public void closeAnimate(int position) {
   touchListener.closeAnimate(position);
 }
Ejemplo n.º 16
0
  /**
   * Init ListView
   *
   * @param attrs AttributeSet
   */
  private void init(AttributeSet attrs) {

    int swipeMode = SWIPE_MODE_BOTH;
    boolean swipeOpenOnLongPress = true;
    boolean swipeCloseAllItemsWhenMoveList = true;
    long swipeAnimationTime = 0;
    float swipeOffsetLeft = 0;
    float swipeOffsetRight = 0;
    int swipeDrawableChecked = 0;
    int swipeDrawableUnchecked = 0;

    int swipeActionLeft = SWIPE_ACTION_REVEAL;
    int swipeActionRight = SWIPE_ACTION_REVEAL;

    if (attrs != null) {
      TypedArray styled = getContext().obtainStyledAttributes(attrs, R.styleable.SwipeListView);
      swipeMode = styled.getInt(R.styleable.SwipeListView_swipeMode, SWIPE_MODE_BOTH);
      swipeActionLeft =
          styled.getInt(R.styleable.SwipeListView_swipeActionLeft, SWIPE_ACTION_REVEAL);
      swipeActionRight =
          styled.getInt(R.styleable.SwipeListView_swipeActionRight, SWIPE_ACTION_REVEAL);
      swipeOffsetLeft = styled.getDimension(R.styleable.SwipeListView_swipeOffsetLeft, 0);
      swipeOffsetRight = styled.getDimension(R.styleable.SwipeListView_swipeOffsetRight, 0);
      swipeOpenOnLongPress =
          styled.getBoolean(R.styleable.SwipeListView_swipeOpenOnLongPress, true);
      swipeAnimationTime = styled.getInteger(R.styleable.SwipeListView_swipeAnimationTime, 0);
      swipeCloseAllItemsWhenMoveList =
          styled.getBoolean(R.styleable.SwipeListView_swipeCloseAllItemsWhenMoveList, true);
      swipeDrawableChecked =
          styled.getResourceId(R.styleable.SwipeListView_swipeDrawableChecked, 0);
      swipeDrawableUnchecked =
          styled.getResourceId(R.styleable.SwipeListView_swipeDrawableUnchecked, 0);
      swipeFrontView = styled.getResourceId(R.styleable.SwipeListView_swipeFrontView, 0);
      swipeBackView = styled.getResourceId(R.styleable.SwipeListView_swipeBackView, 0);
    }

    if (swipeFrontView == 0 || swipeBackView == 0) {
      swipeFrontView =
          getContext()
              .getResources()
              .getIdentifier(SWIPE_DEFAULT_FRONT_VIEW, "id", getContext().getPackageName());
      swipeBackView =
          getContext()
              .getResources()
              .getIdentifier(SWIPE_DEFAULT_BACK_VIEW, "id", getContext().getPackageName());

      if (swipeFrontView == 0 || swipeBackView == 0) {
        throw new RuntimeException(
            String.format(
                "You forgot the attributes swipeFrontView or swipeBackView. You can add this attributes or use '%s' and '%s' identifiers",
                SWIPE_DEFAULT_FRONT_VIEW, SWIPE_DEFAULT_BACK_VIEW));
      }
    }

    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    touchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    touchListener = new SwipeListViewTouchListener(this, swipeFrontView, swipeBackView);
    if (swipeAnimationTime > 0) {
      touchListener.setAnimationTime(swipeAnimationTime);
    }
    touchListener.setRightOffset(swipeOffsetRight);
    touchListener.setLeftOffset(swipeOffsetLeft);
    touchListener.setSwipeActionLeft(swipeActionLeft);
    touchListener.setSwipeActionRight(swipeActionRight);
    touchListener.setSwipeMode(swipeMode);
    touchListener.setSwipeClosesAllItemsWhenListMoves(swipeCloseAllItemsWhenMoveList);
    touchListener.setSwipeOpenOnLongPress(swipeOpenOnLongPress);
    touchListener.setSwipeDrawableChecked(swipeDrawableChecked);
    touchListener.setSwipeDrawableUnchecked(swipeDrawableUnchecked);
    setOnTouchListener(touchListener);
    setOnScrollListener(touchListener.makeScrollListener());
  }
Ejemplo n.º 17
0
 /** Unselected choice state in item */
 public void unselectedChoiceStates() {
   touchListener.unselectedChoiceStates();
 }
Ejemplo n.º 18
0
 /**
  * Count selected
  *
  * @return
  */
 public int getCountSelected() {
   return touchListener.getCountSelected();
 }
Ejemplo n.º 19
0
 /**
  * Get positions selected
  *
  * @return
  */
 public List<Integer> getPositionsSelected() {
   return touchListener.getPositionsSelected();
 }
Ejemplo n.º 20
0
 /**
  * Set action on right
  *
  * @param swipeActionRight Action
  */
 public void setSwipeActionRight(int swipeActionRight) {
   touchListener.setSwipeActionRight(swipeActionRight);
 }
Ejemplo n.º 21
0
 /**
  * Set offset on right
  *
  * @param offsetRight Offset
  */
 public void setOffsetRight(float offsetRight) {
   touchListener.setRightOffset(offsetRight);
 }
Ejemplo n.º 22
0
 /**
  * Sets animation time when user drops cell
  *
  * @param animationTime milliseconds
  */
 public void setAnimationTime(long animationTime) {
   touchListener.setAnimationTime(animationTime);
 }
Ejemplo n.º 23
0
 /**
  * Recycle cell. This method should be called from getView in Adapter when use SWIPE_ACTION_CHOICE
  *
  * @param convertView parent view
  * @param position position in list
  */
 public void recycle(View convertView, int position) {
   touchListener.reloadChoiceStateInView(convertView.findViewById(swipeFrontView), position);
 }
Ejemplo n.º 24
0
 /** Close all opened items */
 public void closeOpenedItems() {
   touchListener.closeOpenedItems();
 }
Ejemplo n.º 25
0
 /**
  * Open ListView's item
  *
  * @param position Position that you want open
  */
 public void openAnimate(int position) {
   touchListener.openAnimate(position);
 }