I can't get my elements, for example ListView
, to resize properly.
I got a ListActivity
for searching, with a dashboard, an editbox and a listview. The idea is to hide the actionbar while the keyboard is showing.
I extended LinearLayout
, to listen to view size changes (as recommended here) :
public class SizeChangingLinearLayout extends LinearLayout {
//...
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
View actionbar = mainView.findViewById(R.id.actionbar);
if (yNew > yOld)
actionbar.setVisibility(View.VISIBLE);
else if (yNew < yOld)
actionbar.setVisibility(View.GONE);
super.onSizeChanged(xNew, yNew, xOld, yOld);
}
}
The hiding/showing of the actionbar works as expected but there is a gap below the ListView
of the same height as the hidden actionbar. As soon as any graphics change (e.g. writing in the editbox) the ListView
fills the gap. A similar behavior appears in reverse, when hiding the keyboard.
[Edit]
The same problem also appears in changing other layout. Changing Visibility
of things in onSizeChanged
shows directly, but changing sizes and margins does not show until some other user action redraws those views. Invalidation does not work from onSizeChanged
.
[/Edit]
I have tried to refresh the graphics by:
invalidate
-ing both the custom
LinearLayout and the ListView
(as
recommended here)notifyDataSetChanged
on the list's
adapterforceLayout
... and more, without success. Why is the ListView
not resizing at first? Do I have to override onLayout
in my extended LinearLayout? What have I missed?
If the position of the object within a parent container changes, but not the size, SizeChanged won't occur. LayoutUpdated is a similar event, but LayoutUpdated is also fired for position changes.
The Adjust Layout feature can do much of that work automatically by adjusting the page elements in your document layout when the page size, page margin, or bleed of the document changes. To adjust the layout for a document:
For example, this might happen if the user has resized the app window and the overall app view is now a narrow view. SizeChanged occurs during initial layout of elements on a page, when the app first is activated, because the ActualHeight and ActualWidth values for UI elements are undefined before layout happens.
Other changes that happen at run-time that change layout space even if they're not directly changing FrameworkElement layout properties. For example, a list that's based on databinding to items might refresh or update, and that could cause size changes in items, items controls, list views, and so on.
Adding the following at the end of onSizeChanged()
will solve it:
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
requestLayout();
}
});
See the answer to this question: Views inside a custom ViewGroup not rendering after a size change
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With