GridView onItemClickListener Not Working? - android

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.

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)

Listview Item only clickable after scrolling

I have a listview with a custom adapter. Everything populates correctly, however, I can only click on a listview item if i scroll the listview up and down. I've tried a bunch of SO answers, but nothing so far has worked, any help would be appreciated.
How do you set the click listener? Do you use listView.setOnItemClickListener(...) or use some view.setOnClickListener() inside the adapter? It seems you use the second approach and set the listener only after the views are reused (using convertView), not immediately after they're inflated. If that's the case check and update the conditions inside the getView() method of the adapter.

onClick listener for dynamic view in Listview inside Fragment

I have the following button-in-Fragment problem:
My custom adapter throws out a row in my listview. That row has a (remove) button.
When the user clicks on the button in the row, the row is removed (and he button as well, duh). I have this working perfectly in an activity. But, how does this work in a Fragment?
If I use findViewbyId in onCreateView it crashes, because the view simply does not exist yet.
onClick in xml is also not an option, that does not work for fragments.
Somewhere I should be able to place a onClick listener and be able to remove it with adapter.remove. But where to place it and what does it look like?
If I understand what you're saying correctly you should add the onCLickListener to the button from within your adapter.
Use onViewCreated() ..it is called after onCreateView, when the view returned has been added.

Handling onItemClick event with ListView and complex layout

This is a request for a best practice for handling the views in the onItemClick events in a ListView.
Each view in my ListView contains an ImageView, some TextViews, and a CheckBox wrapped in a RelativeLayout. On the item view being selected anywhere, I'd like to have the checkbox ticked/unticked as required. I would also like to have the option to change the view style i.e. so the item is highlighted by colouring the stroke.
My current issue is similar to everyone else who does this. Due to ListView recycling, if I grab the View passed into onItemClick and set the backgroundresource or checkbox checked value, it also changes them for Views that aren't currently rendered. I'd like to know the best way to achieve this goal.
I know there is a pattern to handle the CheckBox events in the ArrayAdapter directly by implementing an onClickListener, but I believe this will only respond to direct selection of the CheckBox; it will ignore any clicks on the rest of the view. Plus, if the CheckBox onClick event is fired, it will consume the click event so onItemClick won't fire, which I need to set my data.
Also, using CheckedTextView will not work since it is inside the RelativeLayout, which won't pass the checked event to the ListView. There was a post where it was recommended to create your own CheckedViewLayout that implements Checkable to handle the View checked events. If I go ahead with this, will I be able to reference my new layout class in xml, or would I then have to design each of my views again but in Java code?
If anyone else has a better solution, then please let me know.
Hey I think this link may be useful to you,there is a details for you
Android ListView Checkbox Example - OnItemClickListener() and OnClickListener()
In the end, I created my own CheckableRelativeLayout that implemented Checkable and used it as the main container for each ListView item. I used a Boolean value within CheckableRelativeLayout to track whether the view was clicked or not.
If required, I can post the final class.

Android ListView: How to use the activity onClickListener from a custom Adapter?

I have a listView using a custom adapter. Each row contains a button and some other Views. I want to be able to click either on the button, or on the row itself (to edit the item of the list that is clicked).
Setting an onItemClickListener in the activity won't work because of this problem
I think I have to set an onClickListener in the getView() method of my adapter for it to work properly.
I would like to use my activity's onClickListener, in order to use a startActivityForResult() when the row is clicked, in order to have something returned to my activity when the item edition activity is over.
How can I do that?
Thanks!
You'll need to add an onclick listener to every button you add to every row. The best way to do this is probably to make your own custom layout in code, and every time you create a new view in your adapter, set the onclick listener in the layout code.

Categories

Resources