ListFragment onListItemClick - how to know which component is clicked - android

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?

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

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

Related

Android. ListView. Click on child item in View of ListView disables parent OnClickListener

Ok, now more extended description.
I have ListView element with adapter(extends BaseAdapter).
I have 10+ view elements inside ListView. Adater working as intended, onItemClickListener working good too.
But when inside adapter in getView function I'm creating view element for ListView, I'm add TextView there and set onClickListener for this element.
Testing with Toast message shows me that TextView onClickListener is working. But parent Listener of ListView doesn't anymore.
What's the problem? I understand whole concept, but how to ask android do not stop processing click events after TextView and do ListView click event as well.
I've tried
view.performClick();
view.callOnClick();
view.getRootView.performClick();
view.getRootView.callOnClick();
with no luck
I think you are binding the click listener with viewgroup of whole item that is not the right way. You need to call addView(textview) and then u can initiate textview click listener only. Also if you are a newbie do use recylcerview instead.
Problem solved.
It was wrong to try call view.performClick(); right answer is assuming that we are in adapter ((ListView)parent).performItemClick(view, position, position)

getViewById() in ListView NullPointerException

I have a View in a custom layout of a ListView, now I want to assign an onClickListener to this View (in a Fragment). But if I try
getActivity().findViewById(R.id.rowleft).setOnClickListener( ...
it throws a NullPointerException. Why is this? In the layout file the View has the ID "rowleft" Does the id change when it is in a ListView? Or how do I see which row triggered the event?
What I want is two Views in one Item to know if the user clicked on the left or the right side.
Thank you
Set up your OnClickListeners in your adapter getView() where you're setting up the listview item view anyway.
You can override getView() in an ArrayAdapter. Call super.getView() to get the view to call findViewById() on for the purpose of setting click listeners.
Don't call findViewById() on the activity. Even when the listview items are part of the activity hierarchy, it's not really practical to find views with the same id from different listview items.
ListView.getChildAt(x).findViewById(R.id.rowleft).setOnClickListener( ...

Which: ListView.setOnItemClickListener or TextView.setOnClickListener?

I have a ListView with a custom adapter that only has a TextView, and I want to set a click listener for it.
Which one I should choose: ListView.setOnItemClickListener outside of the custom adapter, or TextView.setOnClickListener inside the custom adapter's getView()? And why?
Thanks!
L.
You should definitely use ListView.setOnItemClickListener
Because when you press the list item it gives feedback that you pressed it (like glow background or something)
You are controlling your data from outside of your list, and therefore you have better vision on the objects you are controlling
More object oriented
In my opinion it's easier
If you have only one view in a list row then why bother setting the click listener on the TextView instead of the row?
adding onClickListener to views in the getView() method is using when you have 2 or more views that should have their own onClickListener,so for your is better onItemClickListener

Detecting which view has been pressed inside list item onClick

I am trying to create an item in a ListView that has multiple options; view and edit. I would like to create it in exactly the same way as android's contact system - see below:
I have added the red boxes to illustrate the behaviour I want. If you press within the left red-box, you call the contact. If you press within the right red-box, you send a text message to the contact. I have already created a similar layout in XML, but I am having trouble implementing this functionality in code.
I have tried to create custom android:onClick function calls for the separate layouts within the item, but calling an onClick method only allows you to pass in the View as a parameter, but not the position. Needing the position to use listview.getItemAtPosition function, I tried to use listview.getPositionForView to return the position but found this was extremely unstable and was very easy to return incorrect positioning due to recycling of views.
What is the best way of implementing a list populated by items with multiple buttons/layouts on each item?
One way to accomplish this while still using a single android:onClick function would be to utilize the setTag() method for each of the clickable views to store an Integer that is the position for the item. For example in your adapter's getView():
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
...
ImageView textMessageView = (ImageView)convertView.findViewById(R.id.whatever);
textMessageView.setTag(new Integer(position));
...
}
Then in your onClick() method you could do something like the following:
#Override
public void onClick(View clickedView) {
int position = clickedView.getTag() instanceof Integer ? ((Integer)clickedView.getTag()).intValue() : -1;
...
}
Don't know that it's ideal, but it allows you to use the XML android:onClick facility and doesn't involve creating separate onClick listeners for each list view item.
I also worked on a similar kind of project.
For doing it, i created my own layout having two linear layouts as linearLayoutLeft and linearLayoutRight.
I then added this layout to a listView.
And then i mentioned the onClickListener of the Listview, i used to getThe view being clicked at the position in the listView.
So for each row in listView, i had two linearLayouts and they have their own onClickListeners.

GridView onItemClickListener Not Working?

This question is often asked here .Lot of people got the solution and working also.
I am also facing the same problem, My gridview onitemclick listner not working.
In my case
I have a viewpager.
Inside that I have fragments. On those fragments I have Gridview. Setting onItemclick listner to grid view not working, tried all case which I found on internet.
This viewpager is itself added in a sepratefragment. See the Picture For better Understanding.
The code is almost simple so not adding here for reference. Where I could be wrong. if not how to hack this
Working case: I am getting pressed events when i am adding onclicklistner to buttons in adapter. But not able to update the button text, when calling notifydatasetchanged.
Sounds like you have buttons inside your gridView adapter. Having buttons, checkboxes etc.. inside the adapter itemView will cause the actual row not to respond to onItemClick events. There are some solutions you would find in fixing this, but what i would suggest is to add a onClickListener to the row (contentView) inside the getView() method of the adapter and handle what you need there. You can pass the position of the clicked view either by a constructor (if you implement the onClickListener into a class which you use for the setOnClickListener or set the position to final in getView and use an anonymous class for the setOnClickListener method call.
If you need to do something based on this click back into the fragment read a bit on how to create a callback with the help of an interface.

Categories

Resources