0

Does anybody know (or know someone else) how to implement the swipe touch interface for a GridView of Android-displayable widgets (i.e. Buttons, ImageView images, and etc.) as opposed to just a GridView of say, a String array? A great example would be Bejeweled or Candy Crush in how the user is able to swap tiles with swipes.

Here's my post in more detail.

Because, I am currently working on an app based on one of the classic puzzle games, Puzzle-15, and I am using direction Buttons (up, down, left, and right) to swap numbered-tiles (Button objects) on the board (GridView)...

Here's my app.

Thanks a bunch!

Community
  • 1
  • 1
DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
  • 1
    The problem is that the event doesn't goes to your gridview ? You can try this solution maybe : http://stackoverflow.com/questions/6374592/passing-touch-events-to-the-parent-view. If it doesn't works you have to find a way to catch the event. You can try to add a transparent view and declare the ontouchlistner in this view. If I didn't understood let me know lol – king Feb 24 '16 at 18:39
  • @user3549047 Thank you so much for the response; it really means a lot to me despite the posts' tediousness... I shall check it out when I have time – DaveNOTDavid Feb 24 '16 at 23:01
  • No problem ;) let me know if you need help – king Feb 24 '16 at 23:17
  • I'm back! So I've looked at the link you posted, but couldn't figure out if I should either implement onInterceptTouchEvent() or maybe even change up my onTouch() method in the swipe touch class... Here's the updated post: http://stackoverflow.com/questions/35852001/how-to-implement-swipe-touch-events-for-gridview – DaveNOTDavid Mar 11 '16 at 15:09
  • Transparent view? Never heard of it. Do you mind providing an example of your suggestion in code? I would greatly appreciate it! :) – DaveNOTDavid Mar 11 '16 at 15:11
  • Let me know my answer helped ;) – king Mar 11 '16 at 18:50
  • Your answer definitely helped! Though, I was wondering how to prevent the swipe functionality in my grid of buttons when I touch anywhere once because it swipes upwards when I do that? – DaveNOTDavid Mar 16 '16 at 13:44
  • 1
    I don't understand your question sorry. When you touch even outside of your screen it swipe ? Or is it because when you touch it swipe anyway ? I think you need to fix a limit. For exemple the user needs to scroll from at least 10 pixels, so if the difference > 10 it's a swipe otherwise he just touched the screen. I said 10 but maybe a touch will move from 10 lol so put 30, you have to test. – king Mar 16 '16 at 14:14
  • Oh no, inside the screen, and yes, when the user touches just once on the screen, it usually swipes upwards. Ok, so if I wanted to raise the swipe minimum for at least 30 pixels, where would I configure that into your CustomView class you provided below? I still have a hard time understanding the class code you provided me... But it works though haha :) – DaveNOTDavid Mar 16 '16 at 14:37
  • 1
    I edited my answer (in the onTouch method) let me know. – king Mar 16 '16 at 14:52
  • Yesssss, it works like a charm! :) – DaveNOTDavid Mar 17 '16 at 16:32

1 Answers1

1

Ok so for the transparent view, here is what I would do:

xml layout (I put my custom view on the button):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test Button"/>

<com.example.trist_000.teststack.CustomView
    android:id="@+id/customv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

You need a custom view that will handle the swipe:

public class CustomView extends View implements View.OnTouchListener {


float save_x = 0;
float save_y = 0;

@Override
public boolean onTouch(View v, MotionEvent event) {


    if (mSwipeInterface == null)
        return false;

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        save_x = event.getX();
        save_y = event.getY();
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        float difx = event.getX() - save_x;
        float dify = event.getY() - save_y;

        if (IsBigger(difx, dify)) {
            if (difx > -30 && difx < 30)return false;
            if (difx > 0){
                mSwipeInterface.swipe(SwipeEnum.RIGHT);
            }
            else{
                mSwipeInterface.swipe(SwipeEnum.LEFT);
            }
        }
        else{
            if (dify > -30 && dify < 30)return false;
            if (dify > 0){
                mSwipeInterface.swipe(SwipeEnum.DOWN);
            }
            else{
                mSwipeInterface.swipe(SwipeEnum.TOP);
            }
        }


    }
    return true;
}

private boolean IsBigger(float difx, float dify) {
    if (difx < 0) {
        difx *= -1;
    }
    if (dify < 0) {
        dify *= -1;
    }

    if (difx > dify){
        return true;
    }
    return false;
}

public enum SwipeEnum {
    LEFT,
    DOWN,
    RIGHT,
    TOP;
}

interface swipeInterface {
    public void swipe(SwipeEnum swipeEnum);
}

private swipeInterface mSwipeInterface = null;

public void setSwipeListener(swipeInterface swipe) {
    mSwipeInterface = swipe;
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(this);
}

public CustomView(Context context) {
    super(context);
}
}

And in the Main Activity:

public class MainActivity extends Activity {


Button mButton;
CustomView mCustomView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    mButton = (Button)findViewById(R.id.button);
    mCustomView = (CustomView)findViewById(R.id.customv);

    mCustomView.setSwipeListener(new CustomView.swipeInterface() {
        @Override
        public void swipe(CustomView.SwipeEnum swipeEnum) {
            if (swipeEnum == CustomView.SwipeEnum.DOWN){
                mButton.setY(200);
            }
            if (swipeEnum == CustomView.SwipeEnum.TOP){
                mButton.setY(0);
            }
            if (swipeEnum == CustomView.SwipeEnum.RIGHT){
                mButton.setX(200);
            }
            if (swipeEnum == CustomView.SwipeEnum.LEFT){
                mButton.setX(0);
            }
        }
    });
}
}

I just put a button, but just put your gridview instead. And replace what I put in the different "if" in the MainActivity with your own logic.

If you have any question about the code let me now.

PS: Algos may not be optimal lol but I tested and it work, you can try ;)

king
  • 507
  • 1
  • 4
  • 17