how to stop horizontal scrolling in case of long press
I have a horizontal list of images. I want, if I long press the image and try to drag the image then.开发者_开发技巧..
Extend the Gallery class and override the onLongPress(MotionEvent e) method and where so ever you are placing Gallery view in the xml place your class instead.
Like the following
package com.prac;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;
import android.widget.Toast;
public class MyGallery extends Gallery {
private Context mContext;
public MyGallery(Context context) {
super(context);
mContext = context;
// TODO Auto-generated constructor stub
}
public MyGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
// TODO Auto-generated constructor stub
}
public MyGallery(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
// TODO Auto-generated constructor stub
}
@Override
public boolean onLongPress(MotionEvent e)
{
// do what ever you want on Long Press Here
return false;
}
}
Now your xml layout should be like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.prac.MyGallery
android:id="@+id/Gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Hope it helps :) And if it helps you out dont forget to accept the answer by clicking the arrow on left side of answer and making it green
精彩评论