ValentinLoricourt
ValentinLoricourt

Reputation: 35

Conflict between my Action_DOWN and my Scrollview

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:

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 !

Upvotes: 1

Views: 1152

Answers (1)

Vaibhav Jani
Vaibhav Jani

Reputation: 12548

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;
    }
};

Upvotes: 1

Related Questions