0

I have a ListView that lives on top of another view.

The very first cell of the ListView is transparent so that you can see the view behind it and then scroll the rest of the contents over it.

I would like the background view to capture touch events only when the transparent cell is on top of it. Is this possible? I tried a million different approaches with overriding dispatch touch event in the first cell or on the listview but haven't had any success.

pat
  • 1,005
  • 4
  • 12
  • 29
  • 1
    Is the ListView a class that you've created? Is the ListView on top of another view, in a FrameLayout [or similar]? Have you tried anything with onTouchEvent? – Matt Dec 09 '13 at 18:57
  • Yes I made a custom ListView class and yes it is inside a FrameLayout. If I override dispatchTouchEvent in the ListView class and return false, everything works (meaning the event passes through correctly) but the listview is not scrollable and no other views are tappable. I haven't tried anything with onTouchEvent yet... – pat Dec 09 '13 at 18:59

2 Answers2

1

When you intercept dispatchTouchEvent, check your ListView, find the top cell (the transparent one), and if the x,y coordinates of the MotionEvent are within that cell, return false. Otherwise return true and your ListView will get the events.

Matt
  • 3,837
  • 26
  • 29
  • Yea I tried something similar, the issue is that those x,y coordinates change. For example if we click at (50, 10) that may be the center of the transparent cell. But then we can scroll the listview up so that the transparent cell is off screen and then a cell that we DO want to click is now at (50,10) and it won't be clickable. Thanks for the suggestion. – pat Dec 09 '13 at 19:19
  • I think you should be able to put a transparent View in the top cell [with a known id] then call findViewById() on it. You could get it's bounds that way. If it's off screen, they should be null, 0, or -1, and if your x,y is inside the view, you know to return false? – Matt Dec 09 '13 at 20:03
  • Also, there's another Method that's likely very helpful: ListView.getFirstVisiblePosition() -- There's some good info at http://stackoverflow.com/questions/2001760/android-listview-get-data-index-of-visible-item/2002413#2002413 – Matt Dec 09 '13 at 20:05
  • Thanks a lot great answer, basically did what you described. Set the bottom of the view in a property of the custom listview onScroll and then checked that vs the motion event inside of dispatchTouchEvent. – pat Dec 10 '13 at 01:16
0

you can also set a tag for the transparent cell and you can get it in onclick() item. you can do anything once you have determine the cell.

nitesh goel
  • 6,338
  • 2
  • 29
  • 38