Add multiple clickable zones in a listview row - java

I want to implement a row for a listview with two click zones with some special requirements:
What I want to implement is something like:
With the following click config:
The red zone links the user profile
The blue zone links the item detail
Clicks over the yellow zone are ignored
When clicking the blue zone all the row is selected (like with a background=?attr/selectableItemBackground)
I have tried several configs but I do not get all the points:
Config A: Add a clicklistener over the blue container and another over the red container, works nice but the full row selected effect when clicking the blue zone is missing.
Config B: Add a clickListener over the full row and another clicklistener over the red container, also works but the clicks over the yellow zone are not ignored.
How can I accomplish that?
Note: Also tried to use the Config B with an empty click listener over the yellow container. But sounds weird to add a clickListener to ignore it, and my real view is more complex than that and I should add a lot of empty clicklisteners. The ideal behavior should be the blue container to propagate its click status to the rowView

You can go with either A or B just with minor addition
A :
Call listview method void setItemsCanFocus(false) so when you click on Blue part, whole row will get focus
B :
Override isEnabled() method in your custom or base adapter and return false for the Yellow part. This will block click events to Yellow part
I hope your error will fix.
Thanks

So A works but you want to get rid the on pressed color ? If so add this in your ListView XML.
<ListView
android:cacheColorHint="#00000000"
android:listSelector="#android:color/transparent"
/>

You should have to implement a layout with Two Sub Layout in which One Contains the Avatar and another contains your data , while in Your Adapter Class You should have to add the tag with Specific Layout id and also add the OnClickListener for that Layout and then while You click the Layout You got the Tag and based on that Tag You are able to handle Your view in ListView Item.

Related

How to add a view to another view in a certain condition?

I'm working on an app and I want to add a view to the current view in a specific location if he checks a CheckBox but every time I try to add parent.addView(child) it crashes .. so I want to know how can I do this and how to release the view when the user clicks the CheckBox again.
That's a demo of what I want to make
https://xd.adobe.com/view/46d4808d-9888-455b-42ca-9fab587eb55d-30e5/
you can set the visibility in the xml file to gone and then set it back to visible when you click on the check box
xml:
android:visibility="gone"
jav:
view.setVisibility(View.VISIBLE)
You can create a Custom Component with the hidden content and reuse it in each checkbox. In addition, you can use a RecyclerView to add the checkbox. Or you can even create the custom component with the checkbox and hidden content and put all the logic there.

How to change items' properties in RecyclerView when chosing a specified item programatically?

I want to change the background colour of an item in a list in RecyclerView using LinearLayoutManager.scrollToPosition(mPosition). I have two buttons which increment or decrement the position number. So, on specifying an item (going to that position using scrollToPosition), I want to highlight it (changing background colour for instance) to indicate the specified one. The case is that we don't have to use touch to scroll up and down and selecting the items (something like using tab key on keyboard to jump between items in the recyclerview).
I've tried OnFocusChanged and assigning an xml instead of background to see the different behaviors like on state_selected or state_focused but none of them was correct. So, What should I do?
If I understand correctly, you want to change the background color after scrolling to this position?
Try this:
LinearLayoutManager.scrollToPosition(mPosition)
View view = LinearLayoutManager.getItem(mPosition);
view.setBackgroundColor(color);

changing listview item elements

I'm new to Android development.
I need to make a listview with text and icon audio player. The icon has three States, "download", "downloading", "play" and "pause". The idea is that when populating the listview, set the "download" icon if the track is not downloaded and "play" if the track is downloaded". If a person clicks the "download" button the icon should be updated to "downloading" to start the download, then when complete, you should see the icon "play". If the user presses the "play" icon for this element should be updated to "pause".
I can't change the icon . I tried to change them in onItemClickListener and make the onClick in the getView method in adapter, but when scrolling the icons changed to the old. And if you select multiple items in the list, then change all the icons, and I need to change only one, for playing of the track.
I didn't show the code because there is nothing that can be corrected. - all wrong
if you have your own adapter try using a method notifyDataSetChanged();
That is happening due to recycling of views. If you click on an item and you will see multiple items clicked.
Use RecyclerView instead of listView, which will handle recycling by its own and its ViewHolder class takes care of the issue you are having.
And don't forget to notify the recyclerView adapter whenever you make some change in data source (i.e. arraylist)
Check out this example...
Hope it helps
http://hmkcode.com/android-simple-recyclerview-widget-example/

Adding another line of components to relativelayout

Anyone know how to get this result?
Description:
When the '+' button is clicked, another line of EditText, Spinner, EditText and Button will be displayed below the line. And lines go on adding if the user click on the '+' button.
Thanks!
I think the best would be to implement a custom ViewGroup extending a LinearLayout in vertical orientation.
You'll register an OnClickListener on the last button: if the row is the last in the list, you'll inflate a new ingredients_item.xml and add it to your view (also you'll refresh the drawable from + to -), otherwise remove the current row.
Your custom view will provide a method like List<Ingredient> getIngredients(), so you'll be able to pass this list to some save() method.
I pushed the demo on GitHub

Android Row becomes Unclickable with Button

I have a listView, where each row has a button in the row layout. However, this seems to make the row itself unclickable. How can I make both the button and row clickable?
Thanks.
You need to set itemsCanFocus on the list like this:
mList.setItemsCanFocus(true);
To make the button clickable. Then you will need to use your own adapter and in getView return a view which is Clickable and focusable. You will also lose the default highlight states so you need to put them back in with the background resource. So do this:
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
to your view before returning your view.
Whenever I see posts concerning the android:focusable and android:clickable attributes, I always see them being both set to the same value at once. I figured there must be a reason if they are two separate attributes instead of being one.
It turns out that a much better way of achieving your desired behavior is to set
android:focusable="false"
or
yourButton.setFocusable(false)
on the Button in your View. Once you do that, you'll be both able to set an OnClickListener on the Button, and a click on the row will fire the onListItemClick() method in your OnItemClickListener.
Try to set your widgets to non clickable and non focusable in xml,the click on items will work normally and also the click on button will work normally.
android:clickable="false"
android:focusable="false"
Hope this helps.
Unfortunately I don't think that is possible. You ListView row can either have focusable widgets, like a button, or be clickable, not both. See link.

Categories

Resources