Difference between Preference's onCreateView and onBindView methods - android

What is the difference between onCreateView and onBindView methods in Preference?
In documentation it says that onBindView:
Binds the created View to the data for this Preference. This is a
good place to grab references to custom Views in the layout and set
properties on them.
Why is it such a good place to set properties on Views in my layout? Currently I am setting properties in onCreateView method and everything seems to work fine.
From my experience it looks like both methods are always called together. Maybe there are some situations when only onBindView is called?

onCreateView() is for creating the View hierarchy that will eventually contain the Preference UI. onBindView() is for binding actual data to that View hierarchy created in onCreateView().
The pattern separates the creation of the View hierarchy - which is cached - from the binding of data to that View hierarchy. In the case of Preference, onCreateView() is only called once, but onBindView() is called every time the UI needs to load the Preference View.
I am guessing that your current setup works because you never change the properties that you set on the Preference. It would be better to setup the properties of the View hierarchy in onBindView(), in case it ever needs to be dynamic.
(As an aside, this View creation vs. binding design pattern is also seen in CursorAdapters, where it only creates enough Views to display on screen, but is constantly binding these Views to new data.)

Related

How to access ViewHolder in RecyclerView with regards to layout calculation

I have created a RecyclerView and have set the proper adapter and LinearLayoutManager. I'm able to access my ViewHolders just fine outside of the onCreate method. During my research of the problem, it appears that in order to access a ViewHolder, the LinearLayoutManager must finish calculating the positions of the views before you're able to use either the findViewHolderForAdapterPosition or findViewHolderForLayoutPosition methods.
I would like to modify only one of the views inside of the many viewholders I add to the RecyclerView.
I'm currently attempting (and failing) at accessing the views/viewholders in any of the Activity life cycle methods (onCreate, onStart, onResume). I see that LinearLayoutManager has a onLayoutCompleted method, but am unsure how to make use of that for my situation.
Is there anyway to gain access to either the views or the viewholders inside the RecyclerView during one of the Activity life cycles or somehow figure out when onLayoutCompleted is called by the LinearLayoutManager? Is there another way around this?
Theoretically I could add all the viewholders into a list inside the adapter and access it that way, but that doesn't seem quite so clean.
Try to hold a list of models inside of the adapter. If you need to change the view, just change the model and call RecyclerView.Adapter.notifyItemChanged(int p) or notifyDataSetChanged().

Android Viewholder implementation

I understand the idea and usage of Viewholder pattern, but still I have one question:
Assume we have one TextView in the viewholder, and 10 items to display ("item0, item1....").
If I call findViewById once, as I understand I have one object of that TextView.
So at first call to getView I inflate the view, find the reference and set text "item0".
At second call I get same TextView and set text "item1" to the same TextView.
Why item 0 text doesn't change?
Is there any cloning in the background?
Is there any cloning in the background?
Android preallocate a number of views that are enough to fill the screen of the device where you are running the app ( a pool of views ), identical from the content perspective but differently from the reference perspective
Assuming that you implement your ViewHolder inside an adapter class and you use the holder in the getView() method, the only thing that is for sure, is that the TextView in your case , describe a slot of the parent structure (e.g. ListView). Once you have defined the slot in an xml, that is inflated from your adapter, there is no cloning or something like that.
According to Google Documentation the holder idea is described as :
Your code might call findViewById() frequently during the scrolling of
ListView, which can slow down performance. Even when the Adapter
returns an inflated view for recycling, you still need to look up the
elements and update them. A way around repeated use of findViewById()
is to use the "view holder" design pattern.
A ViewHolder object stores each of the component views inside the tag
field of the Layout, so you can immediately access them without the
need to look them up repeatedly. First, you need to create a class to
hold your exact set of views.
There is not cloning , only reusability of the view

listener for contentView

is there a listener or some way to determine when contentView is created? I have used other type listeners and they work for other child views inside of content view. however content view is different. I have not yet found anything that works.
Have you tried onContentChanged() yet?
The documentation says
This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView or Window.addContentView).

updating a TextView from another concurrent View

So this one is probably very simple, but I'm having a bit of trouble figuring it out.
So I have a custom View, which is now running in an activity along with a standard TextView widget.
What I want to know is whether there is a way to update the TextView from my custom View class
Define a listener interface in the custom view. Define a setXxxListener() method in the view. Override the listener in the activity class. From the custom view, call the listener when needed. In the activity's listener implementation, update the TextView.
Such is the Java way.
In general, view classes should not make assumptions about other views running along. The object that manages both views - in your case the activity - should coordinate the data exchange between them.

android custom view in expandablelistview with adapter goes nuts

I have a serious problem with a custom view i use in an expandableListView. I use an adapter that extends BaseExpandableListAdapter. The custom view changes its state depending on if it is being consulted or modifying. The state change involves animation and show/hide of ui parts.
The problem is that even thought i let only two views, when i click on the second view i order to make it change its state, the adapter calls the getViewGroup fo the two views but systematically inverting the corresponding model data ids.. .witch makes the ui to animate again...
I precise that i don't use the viewholder pattern since the custom view has its own internal references to the controls to be updated.
Any idea would be appreciated?
i found out the solution, everytime the model changes i called notifyDataChanged() which makes the entire listview to be redrawn

Categories

Resources