I have a custom view in android which is also a container for other views. The child views have certain onClickListeners attached to them. My question is when the parent view is dismissed, does the associated child views and their corresponding listeners also cleaned up or do we need to handle it separately?
Basically any View can be considered as ViewGroup hierarchy with child elements in it. If by "dismissed" your custom View is removed through the call of ViewGroup#removeView (or any similar removal methods) then all its children are removed too.
Related
I created a custom layout which can have an arbitrary amount of child views.
I figured out that the child views are not available when I try to access them in the constructor of my layout view.
So what I currently do is to access them in onMeassure, but it seems to be a bad idea, since this gets called several times.
What is the best place in my layout to init child views? I wish there was a method like onChildViewsAttached(). Any ideas?
Child views are attached to the parent once layout pass is finished, i.e layout() of the view group is finished.
You can also register OnGlobalLayoutListener or OnPredrawListeneron the ViewTreeObserver of your custom layout.
I have a ViewGroup that can have many LinearLayouts. And each LinearLayout can have many nested child views. What I want is that, if any view(even deep nested) inside a LinearLayout gets focus, it should call a custom method of its main parent LinearLayout.
The last thing I want to do is, set OnFocusChangeListener on every single deep nested child views of LinearLayout and that listener will call its parent's custom method. But that is really bad way to go for.
Is there any method I can override of parent LinearLayout that gets called every time its any nested child view's focus changes ?
You could use contentView.getViewTreeObserver().addOnGlobalFocusChangeListener() or override ViewGroup.requestChildFocus() of the root ViewGroup (requestChildFocus() is passed along the view parent chain).
I'm studying android, of course I'm novice, I always read something like view hierarchy, so what exactly does this mean? what is view hierarchy? e.g.
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
http://developer.android.com/reference/android/app/Fragment.html
A View inside another View creates an hierarchy, the outer view becomes the parent of the inner view and the inner view is its child. It's just nested views.
Here's an example:
You'll want to read the docs on the View class, but essentially views can be children of certain other views. You can nest views in complicated ways. This whole structure of views is referred to as the view hierarchy.
http://i.stack.imgur.com/gN6AO.png
Each view in a user interface represents a rectangular area of the display. A view is responsible for what is drawn in that rectangle and for responding to events that occur within that part of the screen (such as a touch event).
A user interface screen is comprised of a view hierarchy with a root view positioned at the top of the tree and child views positioned on branches below. The child of a container view appears on top of its parent view and is constrained to appear within the bounds of the parent view’s display area.
you can refer to this link : [
http://www.techotopia.com/index.php/Understanding_Android_Views,_View_Groups_and_Layouts_in_Android_Studio][1]
I want to implement drag-and-drop in an android application to switch a child view from one custom view to another custom view (of the same type).
My problem is that the OnTouchEvent stops firing when leaving the direct parent while draging (in my case the custom view is build like this: RelativeLayout -> (TextView, Button, LinearLayout -> (*LinearLayouts containing *ImageViews)). I want to show a list of images wrapped in more rows if the images doesn't fit in one row...)
In fact i want to drag one of those imageviews (parent is a linearlayout-row, where the parent is a linearlayout where parent is a relativelayout) to another custom view of the same type. (it just has to be droped over the other view and be added to the other list...) but it always stops receiving the events when leaving its parent linear-layout.
Can you help me understand how the OnTouchEvent is handled when nested in different views? (already tried to add the OnTouchListener to every view and even the rootview of the activity)
To continue to receive touch events outside of your View, call getParent().requestDisallowInterceptTouchEvent(true) from onTouchEvent().
I have a container, usually a common LinearLayout, where I want to add views. After the user clicks on a view, the view should be removed from the container. Obviously, I can implement the functionality in an OnClickListener, and attach it to the view.
But, if another programmer forgets, to remove the view, after doing his stuff in his OnClickListener, the view will remain in the container.
Is there a way, that the container can enforce the removal? I haven't seen a View#getOnClickListener.
Why don't you create your own container and when addView is called you can modify the view's onClickListener.
EDIT:
This is not possible since there is no getOnClickListener and also the variable that holds the listener has the {#hide}.
I guess the only way out if extending each View and overriding the setOnClickListener method.