Swipeable-Cards icon indicating copy to clipboard operation
Swipeable-Cards copied to clipboard

Swipe and Tap

Open dorinsimina opened this issue 9 years ago • 14 comments

Hi,

For setOnClickListener event I would like to preview the card (e.g navigate to a different fragment activity with just the image and zoom in-out functionalities enabled). The problem is that setOnClickListener is called also when trying to swipe left/right.

How can be the setOnClickListener and swipeListeners mutually exclusive?

Dorin

dorinsimina avatar Mar 13 '15 14:03 dorinsimina

I haven't tried this, but off the top of my head this might work:

In the CardContainer class, find the switch case for the different types of motion event. In the ACTION_DOWN event, set a boolean variable to true. In the ACTION_MOVE event, set it to false. Then in the ACTION_UP event, move the click-related code into an if statement checking whether the boolean is true.

BKerestan avatar Mar 13 '15 16:03 BKerestan

@dorinsimina were you able to solve it?

Edit - I solved it by extending the CardStackAdapter and using this code in getCardView - convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startClickTime = Calendar.getInstance().getTimeInMillis(); break; case MotionEvent.ACTION_UP: long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime; if(clickDuration < MAX_CLICK_DURATION) { // do your thing here } } return true; } }); and private static final int MAX_CLICK_DURATION = 200;

mshukla19 avatar Mar 13 '15 17:03 mshukla19

@dorinsimina Were u able to Solve the problem?

@mshukla19 Your Solution is giving me the Null Pointer Exception at setontouchListener

@BKerestan Can u please provide the Sample of your explanation, It would be a big help if it worked.

Thanks. Please Help

sirajsumra92 avatar Mar 25 '15 10:03 sirajsumra92

@sirajsumra92 can you post your adapter code here? i'll have a look. because i'm using this solution only and it's working fine

mshukla19 avatar Mar 25 '15 11:03 mshukla19

@mshukla19 It is not returning Null Pointer now. But the problem is not Solved. There was abstract method getCardView in adapter, I used it like these But the issue is still unsolved:

public View getCardView(int position, CardModel model, View convertView,
        ViewGroup parent) {
    final CardModel finalCardModel = model;
    convertView.setOnTouchListener(new View.OnTouchListener() {
        private long startClickTime;            
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startClickTime = Calendar.getInstance().getTimeInMillis();
                break;
            case MotionEvent.ACTION_UP:
                long clickDuration = Calendar.getInstance()
                        .getTimeInMillis() - startClickTime;
                if (clickDuration < MAX_CLICK_DURATION) {
                    if (finalCardModel.getOnClickListener() != null) {
                        finalCardModel.getOnClickListener().OnClickListener();
                    }
                }
            }
            return true;
        }
    });
    return convertView;
}

Can U pls look if i am doing it right?

sirajsumra92 avatar Mar 25 '15 11:03 sirajsumra92

In the ensureFull method I've added:

view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { CardModel cardModel = (CardModel) getAdapter().getItem(mNextAdapterPosition - 1); if (cardModel.getOnClickListener() != null) { cardModel.getOnClickListener().OnClickListener(); } } });

... and in case MotionEvent.ACTION_DOWN I've commented: CardModel cardModel = (CardModel)getAdapter().getItem(getChildCount()-1); if (cardModel.getOnClickListener() != null) { cardModel.getOnClickListener().OnClickListener(); }

In this way it's detecting the tap event and still able to swipe left/right.

dorinsimina avatar Mar 26 '15 18:03 dorinsimina

Thanks @dorinsimina.. It worked Perfectly. Great Thanks for your answer.

sirajsumra92 avatar Mar 27 '15 06:03 sirajsumra92

@dorinsimina Can u pls help How Can i get the ID or View of the Particular Card Click?

as u see I can implement click listener but can't get View of the Click Listener.

adapter.getCardModel(0).setOnClickListener( new CardModel.OnClickListener() { @Override public void OnClickListener() { // Log.i("Swipeable Cards", // "I am pressing the card");

                                Intent i = new Intent(getActivity(),
                                        Browse_Card.class);
                                startActivity(i);
                            }
                        });

I cant get the Particular ID of the Card. Please help Anyone?

sirajsumra92 avatar Apr 01 '15 10:04 sirajsumra92

In ensureFull you might setup:

view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { CardModel cardModel = (CardModel) getAdapter().getItem(mNextAdapterPosition - 1); if (cardModel.getOnClickListener() != null) { cardModel.getOnClickListener().OnClickListener(); } } });

... and then in your fragment/activity where you are creating the SimpleCardStackAdapter for each added CardModel:

card.setOnClickListener

... also at this point you can maintain a currentIndex and obtain the object displayed now using adapter.getCardModel(currentIndex)

dorinsimina avatar Apr 02 '15 10:04 dorinsimina

@dorinsimina Thanks for your reply. I am still not getting the individual id. Can u please view the issue i posted #50

sirajsumra92 avatar Apr 02 '15 10:04 sirajsumra92

Basically for detecting on which id (connected to a card model) was tapped it's the same with knowing which element was swiped (liked/disliked).

dorinsimina avatar Apr 02 '15 11:04 dorinsimina

On Swipe of First Card, I am getting User ID 23 and again on Swipe of Second Card Still I am getting User ID 23.Though it should return other ID ie. 25.

I have mentioned this in my issue #50. Can u please go through it, It would be a great help.

sirajsumra92 avatar Apr 02 '15 11:04 sirajsumra92

@dorinsimina Thank you man your solution for click listener works... +1 from my side !!

ahmadmiraj avatar Jan 12 '16 13:01 ahmadmiraj

Excuse me guys but where should I extend CardStackAdapter? I can't do it in any activity. If anyone has an example code or could explain it to me please?

avfernandezz avatar Mar 16 '16 16:03 avfernandezz