Update ListView item's drawable state when child clicked - android

The problem:
I have a ListView item with a relatively complex layout. One of the children in the list item view is a TextView with links. The TextView can handle the clicks on links found in it(by using Linkify), and has a OnClickListener that handles click on parts of the text that are not links. The listener will just get the position of the view in the ListView and perform a click:
#Override
public void onClick(View v) {
int position = mListView.getPositionForView(v);
mListView.performItemClick(v, position, mAdapter.getItemId(position));
}
Everything works fine, except the click doesn't trigger the ListView to update the list item's drawable state. It does trigger that when you click on other TextViews in the item that are not clickable.
Thanks!

Got it working.
Set the list item view clickable and set its background as a state-list drawable, also set addStatesFromChildren to true. This will make sure the state of the list view item is changed when any of its children is clicked(assume the child is clickable).
However, this will cause the ListView itself not receiving focus, so you need to handle the click by adding View.OnClickListener to the list item itself instead of adding a OnItemClickedListener to the ListView.

Related

Android: Identify listviewItem when checkbox of item is checked/unchecked

I got a listView with dynamically created items. Each item has a checkbox which can be checked/unchecked. When the checkbox is pressed this function is called:
public void updateClientList(View v) {
}
Now I want to change the specific item in my arraylist which is the base of the listview. But how do I know which checkbox was checked?
How can I do that? Thanks for your help!
If you only have a checkbox, you can use the OnItemClick listener that gives you position besides the view, but with this solution you have to update the checkbox manually so it doesnt consume the item click.
Another way is to use setTag(position) on the views you supply when binding.
You should consider moving on to RecyclerView.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

Handle only one either Recycler view's item click or inside view click

By following Handle Button click inside a row in RecyclerView
and Issue with CardView and OnClickListener in RecyclerView my code is working for both view i.e complete row and imageview inside row.
If imageview inside row is clicked then onclick of row item should not fired, but it happen following approach in given links
Like for ListView item if you give focusable="true" of view inside row then onItemClick is not fired, if view inside row is clicked (only onClick(View) is fired.
How to make it possible with Recycler view?
You may have better success using listeners so that you can "bubble up" those events to your Activity/Fragment. Then you should be able to keep things separate.
Here is an example: https://guides.codepath.com/android/Using-the-RecyclerView#attaching-click-handlers-using-listeners
I followed the tutorial to create one for click on the row, and then created another (on my own) to handle clicking on the button within the row.

list view context menu with list child item focusable android

I have implemented an custom list view with base adapter that contains focusable item like button. so i have disabled the focusable and clickable in xml. My problem is , added context menu for list item in list view using register for context menu. how to make the list view respond for both long item click and also for button list item click?
Get reference to the button and then assign onclicklistener to it and also assign onlongitemclick listener to listview.hope this helps

add buttons at the end of selected listview item in android dynamically?

I have a listview in which my list contain textview,means I have a list of text where user can select the text, read it and set margin note at the end of that text and a button is created over there. So when next time the user read that text then the button/buttons is there and when click on that button a popup is open where he can see his margin note.
Now the problem is that he can create more than one margin note at the end. In this case I have to create one or more button dynamically at the end of that selected textview. So please help me I am unable to create more than one button dynamically.
There are a couple of ways to do it.
1) Create the buttons (max the user will require) already in the xml file of the list item element and set their visibility to invisible or gone. When the user selects it you can set the visibility to visible. The visibility can be set dynamically.
2) The other way is to add buttons programatically. List views do reuse views for performance purposes. this could explain as to why you are having trouble with the button moving positions. in this case you have identify the id of the list item and add and remove buttons every time the view is created in your getView method of your list adapter.
Remember to chk the condition on your getView method and set visibility or add/remove button.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
System.out.println("I clicked row item");
button1= (Button)v.findViewById(R.id.button1);
button1.setVisibility(button1.isShown() ? View.GONE : View.VISIBLE);}

Clicking on List Item also clicks the checkboxes inside item

I've a listview where each listitem has a checkbox. When I click list item, OnItemClick is called but checkbox is also clicked, that's annoying coz checbox onclick actions are triggered.
Edit:
how can I prevent checkbox from being clicked when row Item is clicked?
UPD:
Try to add to root view of the listitem layout this attribute android:descendantFocusability="blocksDescendants".
This should work

Categories

Resources