Swipeable-Cards
Swipeable-Cards copied to clipboard
Swipe and Tap
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
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.
@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;
@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 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 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?
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.
Thanks @dorinsimina.. It worked Perfectly. Great Thanks for your answer.
@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?
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 Thanks for your reply. I am still not getting the individual id. Can u please view the issue i posted #50
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).
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.
@dorinsimina Thank you man your solution for click listener works... +1 from my side !!
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?