Search in sources :

Example 1 with SwipeDismissViewTouchListener

use of it.gmariotti.cardslib.library.view.listener.SwipeDismissViewTouchListener in project cardslib by gabrielemariotti.

the class CardView method setupListeners.

/**
     * Setup All listeners
     */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
protected void setupListeners() {
    //Swipe listener
    if (mCard.isSwipeable()) {
        this.setOnTouchListener(new SwipeDismissViewTouchListener(this, mCard, new SwipeDismissViewTouchListener.DismissCallbacks() {

            @Override
            public boolean canDismiss(Card card) {
                return card.isSwipeable();
            }

            @Override
            public void onDismiss(CardViewWrapper cardView, Card card) {
                final ViewGroup vg = (ViewGroup) (((View) cardView).getParent());
                if (vg != null) {
                    vg.removeView((View) cardView);
                    card.onSwipeCard();
                }
            }
        }));
    } else {
        this.setOnTouchListener(null);
    }
    //OnClick listeners and partial listener
    //Reset Partial Listeners
    resetPartialListeners();
    if (mCard.isClickable()) {
        //Set the onClickListener
        if (!mCard.isMultiChoiceEnabled()) {
            if (mCard.getOnClickListener() != null) {
                this.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mCard.getOnClickListener() != null)
                            mCard.getOnClickListener().onClick(mCard, v);
                    }
                });
            //Prevent multiple events
            //if (!mCard.isSwipeable() && mCard.getOnSwipeListener() == null) {
            //    this.setClickable(true);
            //}
            } else {
                HashMap<Integer, Card.OnCardClickListener> mMultipleOnClickListner = mCard.getMultipleOnClickListener();
                if (mMultipleOnClickListner != null && !mMultipleOnClickListner.isEmpty()) {
                    for (int key : mMultipleOnClickListner.keySet()) {
                        View viewClickable = decodeAreaOnClickListener(key);
                        final Card.OnCardClickListener mListener = mMultipleOnClickListner.get(key);
                        if (viewClickable != null) {
                            //Add listener to this view
                            viewClickable.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    //Callback to card listener
                                    if (mListener != null)
                                        mListener.onClick(mCard, v);
                                }
                            });
                            //Add Selector to this view
                            if (key > Card.CLICK_LISTENER_ALL_VIEW) {
                                mHelperImpl.setCardSelector(viewClickable, getResources().getDrawable(R.drawable.card_selector));
                            }
                        }
                    }
                } else {
                    //There aren't listners
                    this.setClickable(false);
                }
            }
        }
    } else {
        this.setClickable(false);
    }
    //LongClick listener
    if (mCard.isLongClickable()) {
        this.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                if (mCard.getOnLongClickListener() != null)
                    return mCard.getOnLongClickListener().onLongClick(mCard, v);
                return false;
            }
        });
    } else {
        this.setLongClickable(false);
    }
}
Also used : ViewGroup(android.view.ViewGroup) View(android.view.View) CardThumbnailView(it.gmariotti.cardslib.library.view.component.CardThumbnailView) CardHeaderView(it.gmariotti.cardslib.library.view.component.CardHeaderView) SuppressLint(android.annotation.SuppressLint) Card(it.gmariotti.cardslib.library.internal.Card) CardViewWrapper(it.gmariotti.cardslib.library.view.base.CardViewWrapper) SwipeDismissViewTouchListener(it.gmariotti.cardslib.library.view.listener.SwipeDismissViewTouchListener) SuppressLint(android.annotation.SuppressLint)

Example 2 with SwipeDismissViewTouchListener

use of it.gmariotti.cardslib.library.view.listener.SwipeDismissViewTouchListener in project cardslib by gabrielemariotti.

the class CardViewNative method setupListeners.

/**
     * Setup All listeners
     */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
protected void setupListeners() {
    //Swipe listener
    if (mCard.isSwipeable()) {
        this.setOnTouchListener(new SwipeDismissViewTouchListener(this, mCard, new SwipeDismissViewTouchListener.DismissCallbacks() {

            @Override
            public boolean canDismiss(Card card) {
                return card.isSwipeable();
            }

            @Override
            public void onDismiss(CardViewWrapper cardView, Card card) {
                final ViewGroup vg = (ViewGroup) (((View) cardView).getParent());
                if (vg != null) {
                    vg.removeView((View) cardView);
                    card.onSwipeCard();
                }
            }
        }));
    } else {
        this.setOnTouchListener(null);
    }
    //OnClick listeners and partial listener
    //Reset Partial Listeners
    resetPartialListeners();
    if (mCard.isClickable()) {
        //Set the onClickListener
        if (!mCard.isMultiChoiceEnabled()) {
            if (mCard.getOnClickListener() != null) {
                this.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mCard.getOnClickListener() != null)
                            mCard.getOnClickListener().onClick(mCard, v);
                    }
                });
            //Prevent multiple events
            //if (!mCard.isSwipeable() && mCard.getOnSwipeListener() == null) {
            //    this.setClickable(true);
            //}
            } else {
                HashMap<Integer, Card.OnCardClickListener> mMultipleOnClickListner = mCard.getMultipleOnClickListener();
                if (mMultipleOnClickListner != null && !mMultipleOnClickListner.isEmpty()) {
                    for (int key : mMultipleOnClickListner.keySet()) {
                        View viewClickable = decodeAreaOnClickListener(key);
                        final Card.OnCardClickListener mListener = mMultipleOnClickListner.get(key);
                        if (viewClickable != null) {
                            //Add listener to this view
                            viewClickable.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    //Callback to card listener
                                    if (mListener != null)
                                        mListener.onClick(mCard, v);
                                }
                            });
                            //Add Selector to this view
                            if (key > Card.CLICK_LISTENER_ALL_VIEW) {
                                mHelperImpl.setBackground(viewClickable, mHelperImpl.getResourceFromAttrs(getContext(), android.R.attr.selectableItemBackground));
                            }
                        }
                    }
                } else {
                    //There aren't listners
                    this.setClickable(false);
                }
            }
        }
    } else {
        this.setClickable(false);
    }
    //LongClick listener
    if (mCard.isLongClickable()) {
        this.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                if (mCard.getOnLongClickListener() != null)
                    return mCard.getOnLongClickListener().onLongClick(mCard, v);
                return false;
            }
        });
    } else {
        this.setLongClickable(false);
    }
}
Also used : ViewGroup(android.view.ViewGroup) View(android.view.View) CardThumbnailView(it.gmariotti.cardslib.library.view.component.CardThumbnailView) CardHeaderView(it.gmariotti.cardslib.library.view.component.CardHeaderView) SuppressLint(android.annotation.SuppressLint) Card(it.gmariotti.cardslib.library.internal.Card) CardViewWrapper(it.gmariotti.cardslib.library.view.base.CardViewWrapper) SwipeDismissViewTouchListener(it.gmariotti.cardslib.library.view.listener.SwipeDismissViewTouchListener) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 Card (it.gmariotti.cardslib.library.internal.Card)2 CardViewWrapper (it.gmariotti.cardslib.library.view.base.CardViewWrapper)2 CardHeaderView (it.gmariotti.cardslib.library.view.component.CardHeaderView)2 CardThumbnailView (it.gmariotti.cardslib.library.view.component.CardThumbnailView)2 SwipeDismissViewTouchListener (it.gmariotti.cardslib.library.view.listener.SwipeDismissViewTouchListener)2