1

The View Object is from onListItemClick(ListView listView, View view, int position, long id) is RelativeLayout.

However, I want to know which component inside the RelativeLayout is clicked as well for extended functionality.

How do I do that?

theAnonymous
  • 1,701
  • 2
  • 28
  • 62

2 Answers2

2

This answer show why use recyclerview instead of Listview, I think recyclerview will solve your problem stackoverflow.com/a/24933117/2242903

Gustavo Rozolin
  • 1,070
  • 2
  • 13
  • 21
  • 1
    Bit complicated, and the lack of a fully working sample code with multiple components isn't nice... But thanks for pointing me at the right direction. – theAnonymous Jun 01 '16 at 19:48
  • `ListView` and `RecyclerView` are similar. This won't solve his problem. – Gokhan Arik Jun 01 '16 at 22:18
  • @GokhanArik did you read this anser stackoverflow.com/a/24933117/2242903 ? – Gustavo Rozolin Jun 01 '16 at 22:23
  • Yes, I did. It is true, `RecyclerView` doesn't have `onItemClickListener` and it might be better to use `RecyclerView` instead of `ListView`. However, if he wants to use `ListView` he can still achieve the same thing. The answer you posted states that 'The moment you have a click listener for any of the internal elements the callback would not be triggered but it wasn't notified or well documented (if at all) so there was a lot of confusion and SO questions about it.' It was confusing but not impossible. You just have to add `focusable` attributes or block descendants. – Gokhan Arik Jun 01 '16 at 22:38
1

You have to add a listener to every view in RelativeLayout you want to listen. If you want to use a single listener, create one and add it to all of the views you want to listen. In your onClick method, you can put view.getId() to switch statement and determine which one is clicked.

OnItemClickListener won't solve your problem. Neither switching to RecyclerView. They work the same way. It is all about your adapter. If you are using a custom adapter, it doesn't matter you use ListView or RecyclerView. In your adapter, you have access to your components that are in your RelativeLayout. Create a listener as I explained above and add them to your views.

Not the same but similar question: Android: Add event listeners to every item in a ListView

Community
  • 1
  • 1
Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50