Skip to content

Create SwipeMatch#3

Open
equiptice wants to merge 1 commit intomainfrom
equiptice-patch-1
Open

Create SwipeMatch#3
equiptice wants to merge 1 commit intomainfrom
equiptice-patch-1

Conversation

@equiptice
Copy link
Owner

No description provided.

@@ -0,0 +1,2 @@
app/src/main/java/com/example/swipematch/
app/src/main/res/layout/ No newline at end of file
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package com.example.swipematch;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private int currentIndex = 0;
private String[] names = {"Alex", "Sam", "Jamie", "Taylor", "Jordan"};
private int[] images = {
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher
};

private float xDown;
private ImageView cardImage;
private TextView cardText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cardImage = findViewById(R.id.cardImage);
    cardText = findViewById(R.id.cardText);

    updateCard();

    cardImage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    xDown = event.getX();
                    return true;
                case MotionEvent.ACTION_UP:
                    float xUp = event.getX();
                    if (xUp - xDown > 150) {
                        nextCard("Liked");
                    } else if (xDown - xUp > 150) {
                        nextCard("Disliked");
                    }
                    return true;
            }
            return false;
        }
    });
}

private void updateCard() {
    if (currentIndex < names.length) {
        cardImage.setImageResource(images[currentIndex]);
        cardText.setText(names[currentIndex]);
    } else {
        cardText.setText("No more cards!");
        cardImage.setVisibility(View.INVISIBLE);
    }
}

private void nextCard(String action) {
    cardText.setText(action + ": " + names[currentIndex]);
    cardImage.postDelayed(new Runnable() {
        @Override
        public void run() {
            currentIndex++;
            cardImage.setVisibility(View.VISIBLE);
            updateCard();
        }
    }, 500);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant