0

I have a ListView and I created a custom gesture listener to handle fling touches. I had to override onSingleTapConfirmed to let the listener handle the item touch as well. Unfortunately, if I touch an item, it gets highlighted but doesn't get pushed.

I use the following code in the activity:

ListView listView=(ListView)findViewById(android.R.id.list); final GestureDetector gestureDetector = new GestureDetector(new CustomGestureListener(this)); if (listView != null) listView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return false; } return true; } }); 

In the CustomGestureListener:

 @Override public boolean onSingleTapConfirmed(MotionEvent event) { ListView listView=(ListView)((InfoActivity)context).findViewById(android.R.id.list); listView.onTouchEvent(event); return super.onSingleTapUp(event); } 

Also, If I use scroll like this, there is no scroll:

@Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return super.onScroll(e1,e2,distanceX,distanceY); } 

What should I set to let my listview items get clicked, get scrolled and let fling work, too?

2
  • what you want to achieve? Commented Sep 19, 2013 at 12:47
  • I want to handle fling and click exclusively: if the user touches an item, let the item get clicked and start another activity. If the user uses fling, I start an animation. Right now, when a fling occurs, half of the animation is played, then the other activity starts. If I press Back, the other half of the animation is played. I want them to be called according to the movement. Commented Sep 19, 2013 at 12:54

1 Answer 1

2

use this:

ListView lv = new ListView(this); ArrayAdapter<String> a = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); for (int i = 0; i < 20; i++) { a.add("item " + i); } SimpleOnGestureListener listener = new SimpleOnGestureListener() { @Override public boolean onSingleTapConfirmed(MotionEvent e) { Log.d(TAG, "onSingleTapConfirmed "); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.d(TAG, "onFling velocity x " + velocityX); Log.d(TAG, "onFling velocity y " + velocityY); return false; } }; final GestureDetector gd = new GestureDetector(listener); OnTouchListener l = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gd.onTouchEvent(event); } }; lv.setOnTouchListener(l); lv.setAdapter(a); setContentView(lv); 
Sign up to request clarification or add additional context in comments.

6 Comments

If I change my code to return gd.onTouchEvent, and return false in my onSingleTapConfirmed, I get both event fired. I want them exclusively.Any idea?
not sure what you nean. i dont see both events to be fired at the same time
Check my comment at the starting post.
if you want scroll as well then override onScroll in my SimpleOnGestureListener
The problem is that fling and click happens the same time. I want fling OR click reaction. It seems they don't exist without each other.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.