1

As you can see on this image, I have a grey area where there are some imageview. This area is a LinearLayout inside an Horizontal Scrollview. Moreover on each imageview, there is a OnTouchListener which start a drag and drop when there is an ACTION_DOWN.

As you have understand, there is a problem when I try to scroll. Indeed, the ACTION_DOWN is "selected" so I can't scroll .

So I have thought several solutions:

  • To put an higher area and the user can scroll where there is nothing.
  • Not use OnTouchListener but OnLongClickListener

But none of these solutions is good for me. Do you have an idea how I could solve my problem ?

My xml code:

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    android:background="#5d6164"
    android:id="@+id/horizontalScrollView" >

    <LinearLayout
        android:id="@+id/area2_timetable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center">
    </LinearLayout>

</HorizontalScrollView>

My OnTouch method:

    View.OnTouchListener myOnTouchListener = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {

        int action = event.getAction();
        if (action==MotionEvent.ACTION_DOWN)
        {
            SharedPreferences mSharedPrefs = getSharedPreferences("User", Context.MODE_PRIVATE);
            if(mSharedPrefs.getInt("son_active", 0)==1) Son(VariablesManagement.nom_stockage_meal.get(v.getId()));

            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuilder, v, 0);
        }
        return false;
    }
};

I don't think my java code is useful so I didn't put it(in order to have a clear question) but don't hesitate to ask if you think it could help.

Thank you very much !

1 Answers1

1

You need to distinguish between vertical and horizontal swipe.

Try as follow :

private float y1, y2;

//Adjust this threshold as your need
private static final int MIN_DISTANCE = 20; 

View.OnTouchListener myOnTouchListener = new View.OnTouchListener() {

    public boolean onTouch(final View v, MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                y1 = event.getY();

                break;

            case MotionEvent.ACTION_MOVE:

                y2 = event.getY();

                float deltaY = y2 - y1;

                if (Math.abs(deltaY) > MIN_DISTANCE) {

                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);

                    Toast.makeText(getActivity(), "Swiping vertically!", Toast.LENGTH_SHORT).show();

                } else {

                    // Nothing to do
                }

                break;
        }

        return false;
    }
};
Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
  • Thank you for your help ! However there is a problem : I go in "ACTION_DOWN" but never in "ACTION_MOVE" because the imageview never move... – ValentinLoricourt May 10 '16 at 09:56
  • The is no need to move your imageview to get ACTION_MOVE, it indicates "move" of touch not the move of what you touch! Just try it once. – Vaibhav Jani May 10 '16 at 10:00
  • Swipe vertically and you will, I have tested it at my end :) – Vaibhav Jani May 10 '16 at 10:02
  • If I put a Log.d("test", "inside"); just before the y2 I never see it. I'm trying to understand why, I have surely made a mistake. – ValentinLoricourt May 10 '16 at 10:03
  • I tested again. Once your get the focus of ImageView and swipe vertically it starts draging. And I am being able to scroll while swiping horizontally. – Vaibhav Jani May 10 '16 at 10:20
  • Yes all seems logic. I'm have surely made a mistake because I don't see any error in your code. I'm trying to see with the rest of my code if there is not interference. If no, I will try to make this piece of code work on a very basic code. Thank you for your help ! I will let you know as soon as I succeed – ValentinLoricourt May 10 '16 at 10:23
  • 1
    I have found ! The problem come from the return, if the ACTION_DOWN return false so the ACTION_MOVE is not considered(http://stackoverflow.com/a/14776512/6233919). The ACTION_DOWN have to return true and thus, it work perfectly. Thank you very much for your help @Vaibhav A. Jani ! – ValentinLoricourt May 10 '16 at 10:34