Update RecyclerView adapter item without calling OnCreateViewHolder - java

how to update ReyclerView adapter without make OnCreateViewHolder(...) being called?
Because when i do
mRecyclerView.getAdapter().notifiItemChanged(position)
it goes inside OnCreateViewHolder(...) what i don't want to. The reason why i don't want is because I have expanded item view and onCreateViewHolder(...) will reinflate it so make collapsed. I only want to OnBindViewHolder(...) being called
Have anyone faced with it?
UPD:
Just found that OnCreateViewHolder(...) is called only one first time, in all next times it's not being called. What is the reason?
t

You could add a boolean isExpanded to your data objects in your dataset, and then in the onBindViewHolder method check whether isExpanded is true, if so expand the view programmatically. (and ofcourse don't forget to switch the boolean when expanding/collapsing the view)

This is the whole point of RecyclerView : it handle a pool of view holders, which are created once (calling OnCreateViewHolder()) then reused (or recycled, hence the view name) as much as possible via OnBindViewHolder().
OnCreateViewHolder() is used to create the views, while OnBindViewHolder (re)set the views state (like text content, selected state, ...)
It follows that any state associated with an item (like the expanded / collapsed state) should not be stored in the ViewHolder. You can for example use a private List in the adapter.

Related

How to set variable values from a ListView with Checkboxes?

I created a listview with a custom adapter. The items have a checkbox next to them for each item in the array list/listview.
I basically want to set the values of the objects in the array list to true if they are clicked.
Looked in to isChecked() method But I'm having trouble understanding how it checks each checkbox for each item in the list when I only have 1 checkbox in my layout
I have a decent amount of java knowledge but trying to transfer the code I created there into a workable app has proven difficult.
Attaching your implementation would be helpful but here is the general idea:
Your ListView (hopefully RecyclerView, but not mandatory) contains logic for how to lay out particular items in the list of data that fuels it. These reusable layouts and UI components are maintained in the ViewHolder pattern (which explains your single check box in the layout but you see 10 on the screen if your list has 10 items in it). In each ViewHolder you have access to its respective CheckBox so you can setup a listener on the CheckBox to subscribe to its state changes (being checked or unchecked). Inside the listener, you can call a function to update an element in your list. ViewHolders have a nice variable internal to them called adapterPosition which, as you may have guessed, is the VH's position in the adapter (zero based). This is usually super helpful since you can pass this variable around or reference your list directly with the index and it will match up to the correct item in your list.
Hope this helps, and I would be more than happy to assist you with the implementation if you provide some code samples!
You can add one boolean parameter i.e. isSelected to model class that is used in adapter.
add listener to checkbox
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//update isChecked value to model class of list at current position
list.get(position).setChecked(isChecked);
}
});
Then you can easily get checked items from data list of adapter.

How recycler view Data bind?

How Recycler view Bind Data One By One or All in one time?
void onBindViewHolder (VH holder,int position,List<Object> payloads)
Called by RecyclerView to display the data at the specified position.
This method should update the contents of the itemView to reflect the
item at the given position.
So one by one. More here .
Your recycler view calls RecyclerView.Adapter when you set adapter to recycler view or
notify dataset changes to the adapter.
Recycler view calls OnBindViewHolder for each item in your recycler.
Hence, recyclerview binds data one by one.
You can clearly see this when you implement animation to the recycler view items.
RecyclerView is a list drawing library that essentially provides a fixed-size window to load a large dataset into. It recycles the views it created at the begining when the views go out of scope (window) and then if there is a need, reuses them to make it seem as if the views were never offloaded and were virtually present outside the window.It binds the rows which are visible to you and recycles the rest

How to disabling checkboxes inside recyclerview android

I have list/rows of checkboxes inside recycle view. In my case, the user can only select 3 checkboxes out of many. As soon as the user selects the third checkbox, I want to disable rest of the checkboxes in different rows.
I am capturing selecting/deselecting checkboxes inside adapter class.
As far as I know, I can enable/disable checkboxes inside onBindViewHolder class.
But in my case, I want to enable/disable checkboxes, after they have been rendered.
One of the approaches I can think is, to call notifyDataSetChanged(); from Activity class and then rerender all the recycled view.
But I hope there is the better way than doing this, inside the adapter class itself.
ps: I am new to android/java.
notifyDataSetChanged() will defently work in your case.
Please note that you don't have to call this method from the Activity.
You can call it internally within your adapter after the third checkbox was selected.
From the top of my head, One way to do this would be to store the "checked" positions indide an array within the adapter, then inside onBindViewHolder() check to see if the current position should be enabled or disabled.
In your model class you can have a boolean variable that decide whether the item should be enabled or not and another boolean variable to keep track of the checked state, or if the item is checked or not. In onBindViewHolder check the variable to enable or disable the item.Keep track of the selected items and then after every check and uncheck, check if the selected items exceeds the limit. Then loop through your items and then check if the item is checked or not. if the item is not checked set the value to false and after the loop send a notifyDataSetChanged() and in onBindViewHolder() check for the value and enable or disable the item/checkbox
You can use SparseBooleanArray to record all the changes for each item based on their positions.
You can use the following method in RecyclerView adapter:
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)
Read more at RecyclerView.Adapter.
Only use notifyDataSetChanged() as the last resort. Because calling notifyDataSetChanged() will force the views to rebind and relayout. As in the documentation says:
void notifyDataSetChanged ()
Notify any registered observers that the data set has changed.
There are two different classes of data change events, item changes
and structural changes. Item changes are when a single item has its
data updated but no positional changes have occurred. Structural
changes are when items are inserted, removed or moved within the data
set.
This event does not specify what about the data set has changed,
forcing any observers to assume that all existing items and structure
may no longer be valid. LayoutManagers will be forced to fully rebind
and relayout all visible views.
RecyclerView will attempt to synthesize visible structural change
events for adapters that report that they have stable IDs when this
method is used. This can help for the purposes of animation and visual
object persistence but individual item views will still need to be
rebound and relaid out.
If you are writing an adapter it will always be more efficient to use
the more specific change events if you can. Rely on
notifyDataSetChanged() as a last resort.

How to redraw only one view in Adapter

Is it somehow possible to force adapter to redraw only specific item?
Currently if I want to redraw the list, I call notifyDataSetChanged() which re-iterates through all the data-set and redraws all elements. What if I know exactly which element was updated and I want to redraw only that one?
notifyDataSetChanged() is not for redrawing elements, it is for notifying the adapter, that the elements have changed in some way, for example there are more elements, or their data is set from an other class etc. Of course, the ListView (or other class), to which the adapter is connected will redraw the elements, becuase the Adapter is forced to tell him, its dataset is changed.
As Yume117 commented, for redrawing you can call one View's invalidate() method (or even better postInvalidate()),
postInvalidate():
Cause an invalidate to happen on a subsequent cycle
through the event loop.
public void invalidate ()
Added in API level 1
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
You can call these methods on one view in the Adapter's getView() method, or only call invalidate on the ListView (or other view) itself, and it will only redraw the visible elements.
You can create singletone object with Map. And then put every View of your object to this map. And after all, you can change this object, redraw your specified View (you have to know position) and then call notifyDataSetChanged().
There is no such method out of box, which is not so bad as not whole dataset gets re-iterated and not all elements gets redrawn, but just currently visible ones. These are usually not so many.

Is it possible to make a dynamic mix of ListView and TextView?

I tried to found a method, but no results. I want a ListView like below, and when I click on an element, like "Word", it'll be like this picture :
Is it possible ?
What you described is what an ExpandableListView does.
Basically it's like a listview so you'll still have to create your own adapter, but it lets you click a row to inflate a bigger item that you can then stuff your text into.
I would recommend creating your own class that extends ArrayAdapter or BaseAdapter. Then you can make use of the getView() method that gets called every time the screen gets redrawn for the user. You can then design multiple views for each selection and choose which one to display in the getView() function. So when the user selects an item, you set a flag in your custom class, and then notifyDataSetChanged() and you're good to go!

Categories

Resources