6

I have an Android app with a ListActivity in the main view. The list contains a LinearLayout with a TextView and a hidden delete Button. The delete Button will be hidden by default. I want to use a fling gesture to show the button. I am able to detect the fling gesture thanks to question #937313 on stackoverflow. It's not clear to me how to determine which item in the list was flung, since the onTouch listener listens to the ListView. The item is not necessarily selected so getSelected* methods can't be used reliably. I am using the SimpleListAdaptor so I don't have direct access to the View Objects in the ListView.

Any ideas?

Community
  • 1
  • 1
John in MD
  • 2,141
  • 5
  • 25
  • 36

1 Answers1

14

Try using AbsListView.pointToPosition() to determine the list item for the X,Y coordinate in your list view.

If you're using the recipe from #937313, you should be able to override onFling() more or less as follows:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    try {
        Toast.makeText(listAdapter.getItem( listView.pointToPosition( (int) e1.getX(), (int) e1.getY() ).toString() );
        return super.onFling();
    } catch( Exception e ) {
        // do nothing
    }
}
Community
  • 1
  • 1
emmby
  • 99,783
  • 65
  • 191
  • 249
  • 2
    *pointToPosition()* takes int and not float as arguments so use *Math.round(e1.getX()), Math.round(e1.getY())* instead of directly using *e1.getX(), e1.getY()* – FreewheelNat Dec 07 '10 at 11:25