Child view focus change android - android

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).

Related

What happens to child views when parent is dismissed in Android

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.

At what point are child views attached to the parent view

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.

Android detect position of ViewGroup Child

So I really have two related questions on ViewGroups:
Is a LinearLayout and instance of ViewGroup? Meaning, can I call getChildAt(index) when using a LinearLayout?
Can I detect when a ViewGroup child is at the "top" of the screen (right below the action bar) after scrolling? For example, when the 3rd child is scrolled to the top
I'm working on an app that utilizes Parallax Scrolling and I'm using an open source library that has a custom View with a LinearLayout as a child and 4 TextViews as the LinearLayouts children. In their custom View class they are calling getChildAt but I can't seem to find documentation on that related to LinearLayouts so I wanted to check. And essentially I want to check if one of these TextViews are at the top of the screen. Any clairfication would be much appreciated
question 1 { Is a LinearLayout and instance of ViewGroup? Meaning, can I call getChildAt(index) when using a LinearLayout? }
answer = yes... A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers .. so your linear layout is a view because it extends view, and if it contains other sub views called children then it's a viewgroup.. viewgroup extends view.. Meaning YES you can call getChildAt(index) when using linear layout..
question 2 { Can I detect when a ViewGroup child is at the "top" of the screen (right below the action bar)? }.
answer YES , you can check the Z order of the children in a viewgroup(a view containing other sub views) to know its position, so probably your first child is the child below the actionbar, that's if your viewgroup (that is the linear layout) is the view content of your activity - which is always the case right? - yea..
if you want a documentation on getChildAt and viewgroups in general then look here ..
hope it helps.. let me know anything that's arising..

How to use Scroller in ViewGroup (android)

I have a custom ViewGroup and would like to add scrolling ability to it.
Is it possible to use a Scroller object and link it up with view group?
I have read somewhere that Scroller does not do any actual scrolling. That means it must be delegating the scrolling responsibility back to ViewGroup.
thanks
The ScrollView object is what allows for scrolling. Basically, most Views are able to be scrolled, but they have no way of propagating touch events to the View class. A ScrollView handles this for the user, so should be wrapped around a View whenever you would like to enable Scrolling for a that view.
Remember that a ScrollView can only have one child view, so if you need have multiple views in the same scrolling layout, you'll need to have them all inside one LinearLayout (or RelativeLayout, or whatever you decide).

Android: Generic container / Views / View.OnClickListeners

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.

Categories

Resources