Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4053996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:31:05+00:00 2026-05-20T14:31:05+00:00

I have some code that I wrote to implement a vertical swipe on a

  • 0

I have some code that I wrote to implement a vertical swipe on a
Gallery widget. It works great in Android 1.5 and 1.6 but does not
work in Android 2.2 (I have yet to try it with 2.1).

public class SwipeUpDetector extends SimpleOnGestureListener
implements OnTouchListener
{
       private GestureDetector m_detector;

       public SwipeUpDetector()
       {
               m_detector = new GestureDetector(m_context, this);
       }

       @Override
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
       {
               if (Math.abs(e1.getX() - e2.getX()) < s_swipeMaxOffPath &&
                       e1.getY() - e2.getY() >= s_swipeMinDistance &&
                       Math.abs(velocityY) >= s_swipeMinVelocity)
               {
                       int pos = m_gallery.pointToPosition((int)e1.getX(), (int)e2.getY());
                       startAnimation(pos);

                       return true;
               }

               return false;
       }

       @Override
       public boolean onTouch(View v, MotionEvent event)
       {
               return m_detector == null ? false : m_detector.onTouchEvent(event);
       }
}

And to be able to get my gallery to detect the onFling I have the
following:

   m_gallery.setOnTouchListener(new SwipeUpDetector());

In Android 1.5 and 1.6 this works great. In Android 2.2 onFling() is
never called. In looking around on Google and StackOverflow I found
one possible solution was to implement onDown() and return true.

However, I am also listening to single clicks and have a context menu
listener set up on this gallery. When I implement onDown() and return
true I do indeed get the swipe to work. But when I do this the
context menu doesn’t display on a long click and the single clicks
don’t work either… Clicking on items in the gallery cause the
gallery to jump around and I don’t get any feedback when I click on an
item in the gallery. It just immediately makes that item the selected
item and moves it to the center.

I looked at the API differences report between 1.6, 2.1, and 2.2 and
didn’t see anthing of significance that could have caused this to
break…

What am I doing wrong?

EDIT:

It might also be helpful to know that the gallery is nested inside a couple layouts as follows (this isn’t a complete layout… it is just intended to show the hierarchy of where this Gallery lives):

 <ScrollView>
      <LinearLayout>
           <RelativeLayout> <!-- This relative layout is a custom one that I subclassed -->
                <Gallery />
           </RelativeLayout>
      </LinearLayout>
 </ScrollView>

EDIT #2:

Here are the requested layouts… There are two of them, for reusability purposes. Here is the first one, which is the main activity’s layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myns="http://com.magouyaware/appswipe"
    android:id="@+id/main_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:scrollbarAlwaysDrawVerticalTrack="false"
>
    <LinearLayout 
        android:id="@+id/appdocks_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:gravity="center"
        android:background="@null"
    >
        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/running_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/running_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/recent_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/recent_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/favs_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/favs_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/service_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/service_title"
        />

        <com.magouyaware.appswipe.TitledGallery
            android:id="@+id/process_gallery_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            myns:gallery_title="@string/process_title"
        />

        <include 
            android:id="@+id/indeterminate_progress_layout_id" 
            layout="@layout/indeterminate_progress_layout" 
        />
    </LinearLayout>
</ScrollView>

And here is the layout file for com.magouyaware.appswipe.TitledGallery… This is nothing more than a RelativeLayout subclass for the purpose of controlling several views as a single item in the code and for reusability:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titled_gallery_main_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:background="@null"
>
    <LinearLayout
        android:id="@+id/titled_gallery_expansion_layout_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true"
        android:clickable="true"
        android:gravity="center_vertical"
    >
        <ImageView
            android:id="@+id/titled_gallery_expansion_image_id"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:duplicateParentState="true"
            android:clickable="false"
        />

        <TextView
            style="@style/TitleText"
            android:id="@+id/titled_gallery_title_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:paddingLeft="1sp"
            android:paddingRight="10sp"
            android:textColor="@drawable/titled_gallery_text_color_selector"
            android:duplicateParentState="true"
            android:clickable="false"
        />
    </LinearLayout>

    <Gallery
        android:id="@+id/titled_gallery_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/titled_gallery_expansion_layout_id"
        android:layout_alignWithParentIfMissing="true"
        android:spacing="5sp"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:unselectedAlpha=".5"
        android:focusable="false"
    />

    <TextView 
        style="@style/SubTitleText"
        android:id="@+id/titled_gallery_current_text_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/titled_gallery_id"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_horizontal"
    />
</RelativeLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:31:05+00:00Added an answer on May 20, 2026 at 2:31 pm

    I was able to receive single/double clicks and long click as well by implementing onSingleTapConfirmed, onDoubleTap and onLongPress in my implementation of SimpleOnGestureListener (while returning true from onDown).

    Concerning why we should override onDown method. I think it is related to the issue #8233. It was reported one year ago against 2.1 version. Since only 10 people starred it so far I guess it would not be fixed in the near future.

    UPDATE

    It turned out that the issue was caused by the combination of ScrollView and Gallery and usage of OnTouchListener. Gallery itself implements OnGestureListener and encapsulates GestureDetector which is disabled when we set our OnTouchListener, resulting in a strange gallery behavior sometimes. On the other hand if we just subclass Gallery component and perform long-click/swipe detection in its onLongPress/onFling methods the parent ScrollView will intercept vertical move events preventing onFling call for such events. The solution is to override Gallery.dispatchTouchEvent and call requestDisallowInterceptTouchEvent(true) for gallery parent.

    To summarize: if you want to detect swipes (long-, double-clicks etc.) for the Gallery (and possibly place it inside the ScrollView) use the custom component provided below instead of GestureDetector/OnTouchListener.

    public class FlingGallery extends android.widget.Gallery implements OnDoubleTapListener {
    
      private static final int SWIPE_MIN_VELOCITY = 30;   // 30dp, set to the desired value
    
      private static final int SWIPE_MIN_DISTANCE = 50;   // 50dp, set to the desired value
    
      private static final int SWIPE_MAX_OFF_PATH = 40;   // 40dp, set to the desired value
    
      private final float mSwipeMinDistance;
    
      private final float mSwipeMaxOffPath;
    
      private final float mSwipeMinVelocity;
    
      public FlingGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        float density = context.getResources().getDisplayMetrics().density;
        this.mSwipeMinDistance = density * SWIPE_MIN_DISTANCE;
        this.mSwipeMaxOffPath = density * SWIPE_MAX_OFF_PATH;
        this.mSwipeMinVelocity = density * SWIPE_MIN_VELOCITY;
      }
    
      public FlingGallery(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.galleryStyle);
      }
    
      public FlingGallery(Context context) {
        this(context, null);
      }
    
      @Override
      public boolean dispatchTouchEvent(MotionEvent ev) {
        final ViewParent parent;
        if (ev.getAction() == MotionEvent.ACTION_MOVE && (parent = getParent()) != null) {
          parent.requestDisallowInterceptTouchEvent(true);  // this will be passed up to the root view, i.e. ScrollView in our case
        }
        return super.dispatchTouchEvent(ev);
      }
    
      @Override
      public boolean onDoubleTap(MotionEvent e) {
      // Your double-tap handler...
        return true;
      }
    
      @Override
      public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
      }
    
      @Override
      public boolean onSingleTapConfirmed(MotionEvent e) {
      // Your single-tap handler...
        return true;
      }    
    
      @Override
      public void onLongPress(MotionEvent event) {
      // Your long-press handler...
        super.onLongPress(event);
      }
    
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1 == null) {
          return super.onFling(e1, e2, velocityX, velocityY);
        }
        float dx = e2.getX() - e1.getX();
        float dy = e2.getY() - e1.getY();
        if (abs(dx) < mSwipeMaxOffPath && abs(velocityY) > mSwipeMinVelocity && abs(dy) > mSwipeMinDistance) {
          if (dy > 0) {
            // Your from-top-to-bottom handler...
          } else {
            // Your from-bottom-to-top handler...
          }
        } else if (abs(dy) < mSwipeMaxOffPath && abs(velocityX) > mSwipeMinVelocity && abs(dx) > mSwipeMinDistance) {
          if (dx > 0) {
            // Your from-left-to-right handler...
          } else {
            // Your from-right-to-left handler...
          }
        }
        return super.onFling(e1, e2, velocityX, velocityY);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses the shared gateway pattern to implement an inversion
I have some code that effectively does this : File file = new File(C:\\Program
I have a mobile platform that I am trying to write some communications code
I have some code that gives a user id to a utility that then
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that generates image of a pie chart. It's a general
I have some code that I am putting in the code-behind of a master
I have some code that uses the Oracle function add_months to increment a Date
I have some code that prints out databse values into a repeater control on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.