In Android:
Given a View from an onTouchEvent, how can I remove the View and its parent?
I have a LinearLayout in which I have a View. Clicking on the View creates a sub-layout inside of the LinearLayout. That sub-layout needs its own onTouchListeners so I cannot attach an onTouchListener to the parent LinearLayout because that will always block onTouchListeners to my sub-layout.
Therefore, I need a means to remove the parent layout given its child layout. I can already remove child layouts from their parents.
If you know how to remove child views from a parent, stick with what you know ;)
The idea is to elevate to a position in the view tree where you can remove your parent view as a child. How do you do that? Easy, get the grandparent view of your view and remove your parent view from the grandparent.
((ViewGroup)view.getParent().getParent()).removeView((ViewGroup)view.getParent());
Related
I'm confused about the definition. In Android, is a view's parent and container the same thing? For instance, when a recyclerview layout contains a itemview, is the recyclerview both the itemview's parent view and container view?
view's parent and container the same thing?
Yes. Since only containers can contain other views (incl. other containers too), then only said container can be parent to views it contains.
recyclerview layout contains a itemview, is the recyclerview both the itemview's parent view and container view
recyclerview is not the best example, but for simplicity 'yes'. This may give you some overview too:
containerA
view1
containerB
view2
view3
where containerA/B are ViewGroups (i.e. LinearLayouts) and view1/2/3 are i.e. widgets (say Buttons).
I have on LinearLayout (I'll call it the parent) with inside an other LinearLayout (the child)
When I listen to a click on the child layout, I'd like to make the clickable animation on the parent and not only on the child.
How can I make that?
Thank you
One way to do it is to add a OnClickListener to the parent layout and then add the attribute android:duplicateParentState="true" to the inner layout. That way, when you press/click on the inner layout, the parent layout will also receive the event and process any animations and if it was being actioned directly.
I have a child linearlayout created programmatically and added on a parent linearlayour programmatically.
parentlayout.addView(childlayout)
But, the I want to add again the child layout to another linearlayout, it returns an error, is it possible? What is the best way to do that?
Create another instance of the same class, and set the same properties. I mean Inflate your View again if needed. Because A single View cannot be a child of two ViewGroup parents.
Each view can have only one parent possible.
So you can not add same child to another linear layout.
What you can do is make another instance, apply same properties which you given to previous child. and then add this instance to the parent view.
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..
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).