Android: Generic container / Views / View.OnClickListeners - android

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.

Related

Add UI component in fragments dynamically

I have an activity and a fragment. Activity has a trigger event which adds an Imageview to fragment every time event is triggered.
Now I wish to programmatically add image view on every event trigger and refresh the fragment UI.
How can I implement this?
The fragment is just a container for your layout. Basically, you'd have to call addView on the ViewGroup layout that is hosted in the fragment. The ViewGroup could be a LinearLayout for instance. Link to documentation: https://developer.android.com/reference/android/view/ViewManager.html#addView(android.view.View,%20android.view.ViewGroup.LayoutParams)
But as mentioned, you could use a RecyclerView or a ListView and an adapter to accomplish this easier, as it would also save you from messing with LayoutParams, plus it would give you some performance improvements with the help of recycling.
why don't you use a recyclerview then?
Just use a recyclerview and whenever you get the trigger you can add that image dynamically.. or you can inflate image view directly on the parent view.. like: LayoutInflater.from(context).inflate(R.layout.ivLay,'your main layout',true);
ivLay.xml

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.

Android: Lock/Freeze layout for the time changes are made programmatically?

I do reorganization of my layout programmatically adding and removing several views. The problem is after the first removeView the activity/fragment starts to measure/layout the remaining views. Is it possible to put this on hold until I have done all the changes?
Inspired by the post from Arkde I finally found a solution for my problem. I simply override requestLayout() and prevent the call to be propagated to the parent ViewGroup on demand. This enable me to stop the layout process from interfering as long as I reorganize my views.
1st idea
Removes a range of views during layout.The removeView() method looks like this in ViewGroup
public void removeView(View view) {
removeViewInternal(view);
requestLayout();
invalidate(true);
}
The calls to requestLayout() and invalidate() cause your group to trigger measure/layout methods.
In order to avoid this mechanism, you should extend your ViewGroup and override this method, or create a new one to remove chunks of views,and after that trigger requestLayout() and invalidate()
or, if you want to remove all the views starting at a certain position, to another position in layout use,
public void removeViews(int start, int count) {
removeViewsInternal(start, count);
requestLayout();
invalidate(true);
}
that removes a range of views during layout.
2nd idea
If you want your layout to be dynamic and you want to grammatically add or remove parts of the screen, I think you should use Fragments instead.
3rd idea
When you need to change your layout, just create another ViewGroup programatically (or by inflating from xml different layouts), and add all the childs you need there. After that remove all views from current view using ViewGroup.removeAllViews()and add the newly created ViewGroup.

Child view focus change 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).

Difference between View and Subview - Android

What is the difference between a view and a subview in Android?
there is no such thing called a 'subview', its just used to refer to a view inside another view.
View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). and if we insert a view inside the another the its become Subview like a linear Layout containing a button view, here button is a subview
Like Vinay said, there is no such a thing. But you have ViewGroup that contains other Views. For example, LinearLayout, RelativeLayout etc are derived from that class.
If you wish, you can read more about it here:
http://developer.android.com/reference/android/view/ViewGroup.html

Categories

Resources