RecyclerView Item specific attribute OnClick - android

I'm having hard time making this work.
I have the following scenario - when click on CardView the most inner Image should get visible. On second click should disappear. This works fine.
However I want when I select the second CardView the most inner Image to appear the the Image on the first CardView to disappear.
I have this:
1. CardView generated in XML - the cardview have 2 ImageView inside
2. RecyclerView with CustomAdapter and ViewHolder.
3. When I implement OnClick inside ViewHolder it works for each Item - on click the Image appears and on second it disappears.
However I don't know how to check on which Item the image is visible so I can hide it if another Item is clicked. In other word if I select the second I want the first to be deselected. I don't know how to handle this per position.
Any ideas?

Seems like I was missing the point. I solved the problem simply with:
notifyItemChanged(position);
This redraws the item on this position. Since I have simple show/hide behavior with "hide" on initialization I was just using the hide state in OnBindViewHolder and the redraw did the trick.

Related

Expanded and Collapse CardView not working fine

In RecyclerView, there is a problem about the adpter which assigns to every single item the ID. I use a button for every item (are CardView), and this button seems not to have a unique ID, because when I select it, it makes me expand the 1° and 13° item. I also need the expansion of the single cardview I check actually, I've notice that it expands the other cards through the list.
Why? How I can fix it?

Updating RecyclerView item ProgressBar without calling notifyItemChanged(int) on adapter

I am trying to update a progressBar which is the part of one of the RecyclerView items. I know I can do it in two ways,
Method 1: By refreshing the item row by calling notifyItemChanged(position)
Method 2: By accessing the view using the method findViewByPosition() on LayoutMananger object.
But unluckily none of them are working in my case.
If I use the first one, the whole item will get repainted, the background color of the recyclerview is gray and the item is white, so the user can notice the whole item refresh.
If I try the second one, I will end up with problems while scrolling.
I want to update progress just like WhatsApp does. Thanks in advance
If I use the first one, the whole item will get repainted, the background color of the recyclerview is gray and the item is white, so the user can notice the whole item refresh.
I ran into this issue as well. To fix it you need to turn off the default item change animations. Once you turn off the item change animations your recyclerView item will update without any flashing/jumping, allowing you to use notifyItemChanged(int) without any problems.
Turn off the default item change animations like so:
((SimpleItemAnimator) myRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

How to show the specific item add/remove position in recyclerview

I've implemented the RecyclerView concept in studio and am working on adding methods like add and remove items in RecyclerView and it works fine.
My question : while am press the add Button it adds but as a user am not aware of that where it is actually add, in my code am give the position at 1 as static and am scrolled to bottom and click the add button means am not able to know whether it is add or not once am scroll to top only its known. so for this we can able to move the scroll position to the specific item add position during add/remove has been taken.Thanks.
we want to show the exact place of the item is add/removed by scroll the position using following property
recyclerView.scrollToPosition(1);

Change imageview for selected item in listview

In my layout I have a button and a listview. How can I change the imageview of item I selected when the button is clicked. So lets say, I select 5 items, and after I click the button, images for those 5 items will be changed.
So I am confused what function I should use. Right now i used button.setOnClickListener but it seems wrong because only the very first item's imageview will be changed when button clicked. Should I use listview.setItemOnClickListener? Or is there any other way I can do this?
Thanks a lot!
Add a boolean to the data object in your adapter. Say you've got ArrayAdapter<MyDataObject>. Add some kind of "selected" field in the MyDataObject, and toggle it when you "select" the row.
Override getView in the adapter (you'll need a custom Adapter, btw. I'd just extend ArrayAdapter). When you render the row, if the "selected" field is true, show the "other" image.
When you click the button, call 'notifyDataSetChanged' on the adapter. The will cause the visible rows to refresh themselves (and call getView for each).
I think that'll work.
Since you only want images to change when you click the button, you'll need to have some kind of global boolean, so the getView won't show the image until the button has been clicked.
The complication here is, you have to deal with rows that may have been scrolled out of view, which don't have active views, but logically exist. It would be really hard to explain the concept here. I'd suggest some tutorials on ListView if you're not familiar with the recycling of row views.
You can used custom baseadapter for listview and setonclicklistener in the getView() method.... see tutorial here this is hope for help
You can use button click listener as well as on itemClickListener too but to make a image view in selected state in a list, you have to call setSelected method of imageview parent layout.
Please put a comment if you don't get me.
Thanks

android listview

I have created one list view.. it is having 5 items...
Now I want split the list items...
when user clickon the first listitem or focus on first item then immediately it has to show followed some text views or other things..but it has to show same list..
and agian same when he clickon or focus on the second item that first item has to be close and second item has to act some thing....
I think you need to implement the concept of "Expandable Listview", so that the clicking on one item, it will be expanded with their sub-items.
Refer the android-sdk page of Expandable ListView: http://developer.android.com/reference/android/widget/ExpandableListView.html
For having an example, check this site: http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html
Pls, check the below image, do you want to perform as same ????
If you want to do the same, it is already given in the "API-Demos" at Views/Expandable Lists/1. Custom Adapter.
Enjoy !!
The problem is that you cannot use the standard ListView in your case, because in the standard ListView, the View of each row has to be one TextView.
In your case, you need it to be at least two TextViews (The standard Text, and the one that's gonna show up onClick/onFocus).
You have to create your custom ListAdapter, and override the getView() function.
Here is a code snippet that shows how to do it properly:
Custom Adapter
In the getView(), you have to inflate the XML file that describes your List Row, and return it.
In your case, I believe your XML file should contain 2 TextViews, one visible and one invisible.
Then, to handle the clicks, you can set a onItemClickListener on your ListView in your Activity class.
The best way may be to have your Activity class implementing onItemClickListener, and to use this onItemClickListener to handle those.
In the onClick() function, you just have to set the Visibility of your hidden TextView to VISIBLE.
You need to build custom rows, and handle showing more text on each row, there is no easy magicall way of doing it, but inflating your own rows, and setting a couple of attributes visibility isnt all that hard either.

Categories

Resources