GONE view with screen space

I have a problem when views with the GONE visibility state (undesirable) take up space on the screen. This problem always occurs at the API level <= 7, but only recently on 8+ devices (after I used AsyncTasks to fill in some fields, according to the Show progress bar when the action loads )

A little context: I created a custom view that extends LinearLayout, which contains a "title" button and (the user defined, in some cases, several TextViews, in others TableLayouts) "content". The purpose of this view is to switch the content view to the β€œClick” button of the header button (I don’t believe that there is a built-in widget for this. Maybe I'm wrong).

In onLayout (), I explicitly set the visibility state of all child views except the GONE header when it is first accessed:

protected void onLayout(boolean changed, int l, int t, int r, int b) { if(initialDraw) { setContentsVisible(false); initialDraw = false; } super.onLayout(changed, l, t, r, b); } public void setContentsVisible(boolean visible) { for(int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if(child != mTitle) { child.setVisibility(visible ? VISIBLE : GONE); } } } 
+7
android android-layout android-widget
source share
3 answers

Moving the code from onLayout () to onMeasure () solves the problem.

+1
source share

Ok I do not understand how this generally paints in your situation. But I think you should call super.onLayout () after checking. Like this:

 protected void onLayout(boolean changed, int l, int t, int r, int b) { if(initialDraw) { setContentsVisible(false); initialDraw = false; } super.onLayout(l,t,r,b); } 

LinearLayout decides how to place its children (vertically or horizontally) in onLayout () that you didn't call. Moreover, after choosing, he actually begins to figure out how to arrange each child, and if the child’s visibility is GONE, he skips it (this means that the child will not occupy any space). I think that in your case not a single child will be laid out and shown at all, but I may be something wrong.

0
source share

I suspect you are making your life unnecessarily difficult. Make the extensible part of your view (a bit without a title with multiple TextViews or TableLayouts) belong to the same View container - FrameLayout seems to be the likely choice - and just call setVisibility() .

No need to go in cycles, and you definitely don't want to do this from onLayout () ... just do it from your onClick() name.

0
source share

All Articles