Search in sources :

Example 1 with Card

use of it.gmariotti.cardslib.library.internal.Card in project cardslib by gabrielemariotti.

the class SwipeDismissListViewTouchListener method onTouch.

/**
 * Returns an {@link AbsListView.OnScrollListener} to be added to the {@link
 * ListView} using {@link ListView#setOnScrollListener(AbsListView.OnScrollListener)}.
 * If a scroll listener is already assigned, the caller should still pass scroll changes through
 * to this listener. This will ensure that this {@link SwipeDismissListViewTouchListener} is
 * paused during list view scrolling.</p>
 *
 * @see SwipeDismissListViewTouchListener
 */
/*
    public AbsListView.OnScrollListener makeScrollListener() {
        return new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                setEnabled(scrollState != AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
            }
        };
    }*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }
    switch(motionEvent.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            {
                if (mPaused) {
                    return false;
                }
                if (mSwiping) {
                    return true;
                }
                // TODO: ensure this is a finger, and set a flag
                // Find the child view that was touched (perform a hit test)
                Rect rect = new Rect();
                int childCount = mListView.getChildCount();
                int headerCount = mListView.getHeaderViewsCount();
                int footerCount = mListView.getFooterViewsCount();
                int[] listViewCoords = new int[2];
                mListView.getLocationOnScreen(listViewCoords);
                int x = (int) motionEvent.getRawX() - listViewCoords[0];
                int y = (int) motionEvent.getRawY() - listViewCoords[1];
                View child = null;
                for (int i = headerCount; i < (childCount - footerCount); i++) {
                    child = mListView.getChildAt(i);
                    child.getHitRect(rect);
                    if (rect.contains(x, y)) {
                        mDownView = child;
                        break;
                    }
                }
                if (mDownView != null) {
                    mDownX = motionEvent.getRawX();
                    mDownY = motionEvent.getRawY();
                    mDownPosition = mListView.getPositionForView(mDownView);
                    if (mDownPosition != ListView.INVALID_POSITION && mDownPosition < mListView.getAdapter().getCount()) {
                        if (mListView.getAdapter().getItem(mDownPosition) instanceof Card) {
                            if (mCallbacks.canDismiss(mDownPosition, (Card) mListView.getAdapter().getItem(mDownPosition))) {
                                mVelocityTracker = VelocityTracker.obtain();
                                mVelocityTracker.addMovement(motionEvent);
                            } else {
                                mDownView = null;
                            }
                        } else {
                            mDownView = null;
                        }
                    } else {
                        mDownView = null;
                    }
                }
                view.onTouchEvent(motionEvent);
                return true;
            // return false;
            }
        case MotionEvent.ACTION_UP:
            {
                if (mVelocityTracker == null) {
                    break;
                }
                float deltaX = motionEvent.getRawX() - mDownX;
                mVelocityTracker.addMovement(motionEvent);
                mVelocityTracker.computeCurrentVelocity(1000);
                float velocityX = mVelocityTracker.getXVelocity();
                float absVelocityX = Math.abs(velocityX);
                float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
                boolean dismiss = false;
                boolean dismissRight = false;
                if (Math.abs(deltaX) > mViewWidth / swipeDistanceDivisor && mSwiping) {
                    dismiss = true;
                    dismissRight = deltaX > 0;
                } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && mSwiping) {
                    // dismiss only if flinging in the same direction as dragging
                    dismiss = (velocityX < 0) == (deltaX < 0);
                    dismissRight = mVelocityTracker.getXVelocity() > 0;
                }
                if (dismiss && mDownPosition != ListView.INVALID_POSITION) {
                    // dismiss
                    dismiss(mDownView, mDownPosition - mListView.getHeaderViewsCount(), dismissRight);
                } else {
                    // cancel
                    mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = null;
                mDownPosition = ListView.INVALID_POSITION;
                if (mSwiping) {
                    // To prevent onClick event with a fast swipe
                    mSwiping = false;
                    return true;
                }
                mSwiping = false;
                break;
            }
        case MotionEvent.ACTION_CANCEL:
            {
                if (mVelocityTracker == null) {
                    break;
                }
                if (mDownView != null) {
                    // cancel
                    mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
                }
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                mDownX = 0;
                mDownY = 0;
                mDownView = null;
                mDownPosition = ListView.INVALID_POSITION;
                mSwiping = false;
                break;
            }
        case MotionEvent.ACTION_MOVE:
            {
                if (mVelocityTracker == null || mPaused) {
                    break;
                }
                mVelocityTracker.addMovement(motionEvent);
                float deltaX = motionEvent.getRawX() - mDownX;
                float deltaY = motionEvent.getRawY() - mDownY;
                boolean movementAllowed = isSwipeMovementAllowed(deltaX);
                if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2 && movementAllowed) {
                    mSwiping = true;
                    mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
                    mListView.requestDisallowInterceptTouchEvent(true);
                    // Cancel ListView's touch (un-highlighting the item)
                    MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                    mListView.onTouchEvent(cancelEvent);
                    view.onTouchEvent(cancelEvent);
                    cancelEvent.recycle();
                }
                if (mSwiping) {
                    mDownView.setTranslationX(deltaX - mSwipingSlop);
                    mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
                    return true;
                }
                break;
            }
    }
    return false;
}
Also used : Rect(android.graphics.Rect) AbsListView(android.widget.AbsListView) View(android.view.View) ListView(android.widget.ListView) Card(it.gmariotti.cardslib.library.internal.Card) MotionEvent(android.view.MotionEvent)

Example 2 with Card

use of it.gmariotti.cardslib.library.internal.Card in project cardslib by gabrielemariotti.

the class CardArrayRecyclerViewAdapter method remove.

/**
 * Removes the element at position
 * @param position
 * @return
 */
@NonNull
@Override
public Card remove(final int position) {
    Card result = mCards.remove(position);
    notifyItemRemoved(position);
    return result;
}
Also used : Card(it.gmariotti.cardslib.library.internal.Card) NonNull(android.support.annotation.NonNull)

Example 3 with Card

use of it.gmariotti.cardslib.library.internal.Card in project cardslib by gabrielemariotti.

the class MultiChoiceAdapterHelperBase method internal_onItemClick.

public void internal_onItemClick(AdapterView<?> parent, View view, int position, long id) {
    MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;
    Card mCard = adapter.getItem(position);
    if (mCard != null && mCard.getOnClickListener() != null)
        mCard.getOnClickListener().onClick(mCard, view);
}
Also used : Card(it.gmariotti.cardslib.library.internal.Card)

Example 4 with Card

use of it.gmariotti.cardslib.library.internal.Card in project cardslib by gabrielemariotti.

the class MultiChoiceAdapterHelperBase method onItemCheckedStateChanged.

/**
 * Called when an item is checked or unchecked during selection mode.
 *
 * @param mode The {@link ActionMode} providing the selection mode
 * @param position Adapter position of the item that was checked or unchecked
 * @param id Adapter ID of the item that was checked or unchecked
 * @param checked <code>true</code> if the item is now checked, <code>false</code>
 *                if the item is now unchecked.
 */
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
    onItemSelectedStateChanged(mode);
    MultiChoiceAdapter adapter = (MultiChoiceAdapter) owner;
    Card card = adapter.getItem(position);
    adapter.onItemCheckedStateChanged(mode, position, id, checked, card.getCardView(), card);
}
Also used : Card(it.gmariotti.cardslib.library.internal.Card)

Example 5 with Card

use of it.gmariotti.cardslib.library.internal.Card in project cardslib by gabrielemariotti.

the class StickyCardArrayAdapter method getHeaderView.

@Override
public View getHeaderView(int position, View convertView, ViewGroup viewGroup) {
    // Build your custom HeaderView
    // In this case I will use a Card, but you can use any view
    LayoutInflater mInflater = LayoutInflater.from(getContext());
    View view = mInflater.inflate(R.layout.carddemo_extras_sticky_header, null);
    CardViewWrapper cardView = (CardViewWrapper) view.findViewById(R.id.carddemo_card_sticky_header_id);
    Card card = getItem(position);
    char headerChar = card.getTitle().subSequence(0, 1).charAt(0);
    ColorCard colorCard = new ColorCard(getContext());
    colorCard.setTitle("Header : " + headerChar);
    switch(position / 8) {
        case 0:
            colorCard.setBackgroundResourceId(R.drawable.demoextra_card_selector_color1);
            break;
        case 1:
            colorCard.setBackgroundResourceId(R.drawable.demoextra_card_selector_color2);
            break;
        case 2:
            colorCard.setBackgroundResourceId(R.drawable.demoextra_card_selector_color3);
            break;
        case 3:
            colorCard.setBackgroundResourceId(R.drawable.demoextra_card_selector_color4);
            break;
        case 4:
            colorCard.setBackgroundResourceId(R.drawable.demoextra_card_selector_color5);
            break;
        default:
            colorCard.setBackgroundResourceId(R.drawable.demoextra_card_selector_color1);
            break;
    }
    cardView.setCard(colorCard);
    return view;
}
Also used : LayoutInflater(android.view.LayoutInflater) CardViewWrapper(it.gmariotti.cardslib.library.view.base.CardViewWrapper) CardListView(it.gmariotti.cardslib.library.view.CardListView) View(android.view.View) ColorCard(it.gmariotti.cardslib.demo.extras.cards.ColorCard) Card(it.gmariotti.cardslib.library.internal.Card) ColorCard(it.gmariotti.cardslib.demo.extras.cards.ColorCard)

Aggregations

Card (it.gmariotti.cardslib.library.internal.Card)134 ArrayList (java.util.ArrayList)53 CardHeader (it.gmariotti.cardslib.library.internal.CardHeader)51 BaseCard (it.gmariotti.cardslib.library.internal.base.BaseCard)39 View (android.view.View)35 CardView (it.gmariotti.cardslib.library.view.CardView)31 CardViewNative (it.gmariotti.cardslib.library.view.CardViewNative)31 CustomExpandCard (it.gmariotti.cardslib.demo.cards.CustomExpandCard)26 CardArrayAdapter (it.gmariotti.cardslib.library.internal.CardArrayAdapter)26 CardListView (it.gmariotti.cardslib.library.view.CardListView)24 CustomCard (it.gmariotti.cardslib.demo.cards.CustomCard)22 CustomHeaderInnerCard (it.gmariotti.cardslib.demo.cards.CustomHeaderInnerCard)22 ScrollView (android.widget.ScrollView)15 TextView (android.widget.TextView)14 CardExpand (it.gmariotti.cardslib.library.internal.CardExpand)14 MenuItem (android.view.MenuItem)10 CardThumbnail (it.gmariotti.cardslib.library.internal.CardThumbnail)9 CustomThumbCard (it.gmariotti.cardslib.demo.cards.CustomThumbCard)8 PicassoCard (it.gmariotti.cardslib.demo.extras.cards.PicassoCard)7 CardRecyclerView (it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView)7