Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Usage
android:id="@+id/carouselView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:longClickable="true"
app:fillColor="#FFFFFFFF"
app:pageColor="#00000000"
app:radius="6dp"
Expand Down Expand Up @@ -111,6 +112,11 @@ customCarouselView.setImageClickListener(new ImageClickListener() {
@Override
public void onClick(int position) {
Toast.makeText(SampleCarouselViewActivity.this, "Clicked item: "+ position, Toast.LENGTH_SHORT).show();
}

@Override
public void onLongClick(int position) {
Toast.makeText(SampleCarouselViewActivity.this, "Long clicked item: "+ position, Toast.LENGTH_SHORT).show();
}
});
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.synnapps.carouselview;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.animation.Interpolator;

import java.lang.reflect.Field;

/**
* Created by Sayyam on 3/28/16.
*
* Edited by sqljim on 4/14/19
*/
public class CarouselViewPager extends ViewPager {

private ImageClickListener imageClickListener;
private float oldX = 0, newX = 0, sens = 5;
private float oldX = 0, newX = 0, oldY = 0, newY = 0, sens = 5;
private boolean longClick;



public void setImageClickListener(ImageClickListener imageClickListener) {
this.imageClickListener = imageClickListener;
Expand Down Expand Up @@ -58,23 +65,49 @@ public void setTransitionVelocity(int scrollFactor) {
mScroller.setmScrollDuration(scrollFactor);
}

final Handler handler = new Handler();

Runnable mlongClicked = new Runnable() {
public void run() {
longClick = true;
}
};

@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
oldX = ev.getX();
oldY = ev.getY();

//This is where we start a timer, when the timer runs out the longClick boolean
//is set to true, signifying a long press has occurred.
longClick = false;
handler.postDelayed(mlongClicked, ViewConfiguration.getLongPressTimeout());
break;

//The below detects whether a click/longClick has occurred
//or whether a line has been drawn
case MotionEvent.ACTION_UP:
newX = ev.getX();
if (Math.abs(oldX - newX) < sens) {
newY = ev.getY();

handler.removeCallbacks(mlongClicked);
if (Math.abs(oldX - newX) < sens && !longClick) {
if(imageClickListener != null)
imageClickListener.onClick(getCurrentItem());
return true;
}
else if (Math.abs(oldX - newX) < sens && Math.abs(oldY - newY) < sens && longClick)
{
if(imageClickListener != null)
imageClickListener.onLongClick(getCurrentItem());
return true;
}
oldX = 0;
newX = 0;
break;

}

return super.onTouchEvent(ev);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

public interface ImageClickListener {
void onClick(int position);

void onLongClick(int position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClick(int position) {
Toast.makeText(SampleCarouselViewActivity.this, "Clicked item: "+ position, Toast.LENGTH_SHORT).show();
}

@Override
public void onLongClick(int position) {
Toast.makeText(SampleCarouselViewActivity.this, "Long clicked item: "+ position, Toast.LENGTH_SHORT).show();
}

});

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#444"
android:longClickable="true"
app:animateOnBoundary="true"
app:autoPlay="true"
app:fillColor="#FFFFFFFF"
Expand Down