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
2 changes: 1 addition & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
compile 'com.jakewharton:butterknife:5.1.2'
compile 'com.jakewharton:butterknife:7.0.1'
}
57 changes: 32 additions & 25 deletions example/src/main/java/com/lorentzos/swipecards/MyActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,38 @@
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.lorentzos.flingswipe.SwipeFlingAdapterView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class MyActivity extends Activity {

private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;
private boolean left_enable_switch = false;
private boolean right_enable_switch = false;

@InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;

@Bind(R.id.frame) SwipeFlingAdapterView flingContainer;
@Bind(R.id.left_enable_switch) ToggleButton lToggle;
@Bind(R.id.right_enable_switch) ToggleButton rToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButterKnife.inject(this);

ButterKnife.bind(this);

al = new ArrayList<>();
al.add("php");
Expand All @@ -45,9 +49,16 @@ protected void onCreate(Bundle savedInstanceState) {

arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al );


flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {

final SwipeFlingAdapterView.onFlingListener listener =
new SwipeFlingAdapterView.onFlingListener() {

@Override
public Pair<Boolean,Boolean> isEnabled() {
return new Pair<>(left_enable_switch,right_enable_switch);
}

@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Expand Down Expand Up @@ -84,8 +95,9 @@ public void onScroll(float scrollProgressPercent) {
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
};

flingContainer.setFlingListener(listener);

// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
Expand All @@ -95,27 +107,22 @@ public void onItemClicked(int itemPosition, Object dataObject) {
}
});

lToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
left_enable_switch = isChecked;
}
});

rToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
right_enable_switch = isChecked;
}
});
}

static void makeToast(Context ctx, String s){
Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show();
}


@OnClick(R.id.right)
public void right() {
/**
* Trigger the right event manually.
*/
flingContainer.getTopCardListener().selectRight();
}

@OnClick(R.id.left)
public void left() {
flingContainer.getTopCardListener().selectLeft();
}




}
14 changes: 8 additions & 6 deletions example/src/main/res/layout/buttons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<Button
android:id="@+id/left"
<ToggleButton
android:id="@+id/left_enable_switch"
android:layout_margin="10dp"
android:text="Left"
android:textOff="Enable Left"
android:textOn="Disable Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/right"
<ToggleButton
android:id="@+id/right_enable_switch"
android:layout_margin="10dp"
android:text="Right"
android:textOff="Enable Right"
android:textOn="Disable Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.animation.AnimatorListenerAdapter;
import android.graphics.PointF;
import android.util.Log;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -70,7 +71,6 @@ public FlingCardListener(View frame, Object itemAtPosition, float rotation_degre

}


public boolean onTouch(View view, MotionEvent event) {

switch (event.getAction() & MotionEvent.ACTION_MASK) {
Expand Down Expand Up @@ -146,13 +146,12 @@ public boolean onTouch(View view, MotionEvent event) {
final float dx = xMove - aDownTouchX;
final float dy = yMove - aDownTouchY;


// Move the frame
float distobjectX = aPosX - objectX;
aPosX += dx;
aPosY += dy;

// calculate the rotation degrees
float distobjectX = aPosX - objectX;
float rotation = BASE_ROTATION_DEGREES * 2.f * distobjectX / parentWidth;
if (touchPosition == TOUCH_BELOW) {
rotation = -rotation;
Expand All @@ -163,6 +162,7 @@ public boolean onTouch(View view, MotionEvent event) {
frame.setY(aPosY);
frame.setRotation(rotation);
mFlingListener.onScroll(getScrollProgressPercent());

break;

case MotionEvent.ACTION_CANCEL: {
Expand All @@ -187,11 +187,16 @@ private float getScrollProgressPercent() {
}

private boolean resetCardViewOnStack() {
if (movedBeyondLeftBorder()) {

Pair<Boolean,Boolean> swipeEnablers = mFlingListener.isEnabled();
Boolean leftSwipeEnable = swipeEnablers.first;
Boolean rightSwipeEnable = swipeEnablers.second;

if (movedBeyondLeftBorder() && leftSwipeEnable) {
// Left Swipe
onSelected(true, getExitPoint(-objectW), 100);
mFlingListener.onScroll(-1.0f);
} else if (movedBeyondRightBorder()) {
} else if (movedBeyondRightBorder() && rightSwipeEnable) {
// Right Swipe
onSelected(false, getExitPoint(parentWidth), 100);
mFlingListener.onScroll(1.0f);
Expand Down Expand Up @@ -334,6 +339,8 @@ public PointF getLastPoint() {
}

protected interface FlingListener {
Pair<Boolean,Boolean> isEnabled();

void onCardExited();

void leftExit(Object dataObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.Gravity;
import android.view.View;
import android.widget.Adapter;
Expand Down Expand Up @@ -203,8 +204,6 @@ private void makeAndAddView(View child) {
}




/**
* Set the top view and add the fling listener
*/
Expand All @@ -217,6 +216,11 @@ private void setTopView() {
flingCardListener = new FlingCardListener(mActiveCard, mAdapter.getItem(0),
ROTATION_DEGREES, new FlingCardListener.FlingListener() {

@Override
public Pair<Boolean,Boolean> isEnabled() {
return mFlingListener.isEnabled();
}

@Override
public void onCardExited() {
mActiveCard = null;
Expand Down Expand Up @@ -323,6 +327,7 @@ public interface OnItemClickListener {
}

public interface onFlingListener {
Pair<Boolean,Boolean> isEnabled();
void removeFirstObjectInAdapter();
void onLeftCardExit(Object dataObject);
void onRightCardExit(Object dataObject);
Expand Down