I have a custom ListView class (subclassed from ListView) and I need it to add a small padding to the bottom of the final view element so that it is not overlapped by a small bar that I have across the bottom of the screen. I only want to do this when the child views grow past the visible region of the listview. I am trying to use this code to achieve this:
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// If there are hidden listview items we need to add a small padding to the last one so that it
// partially hidden by the bottom sliding drawer handle
if (this.getLastVisiblePosition() - this.getFirstVisiblePosition() + 1 > this.getCount()) {
LinearLayout v2 = (LinearLayout) this.getChildAt(this.getCount() - 1);
v2.setPadding(v2.getPaddingLeft(), v2.getPaddingTop(), v2.getPaddingRight(),
v2.getPaddingBottom() + 5);
}
}
However, the values returned by getLastVisiblePostion(), getFirstVisiblePostion() and getCount() do not reflect what the adapter holds yet. I am assuming this is because the Adapter has not yet notified the ListView of the data, but I cannot figure out where the ListView would actually know about the data and thus have the correct values. This code is being run when the Activity is being loaded.
At what point in the rendering process will I have access to this data? I should also say that I use an AsyncTask to load the data from a database and then create the Adapter in there and add it to the list view. Is there an event I can use within the ListView that will fire when the adapter adds data/causes the listview to render the new items?
I’m not sure if it’s the best solution, but I know that in one of the branches of a pull-to-refresh implementation for Android, the height of the list is compared against the total height of all the list items. From what you’re saying, it sounds like that’s the same information you’re looking for in order to determine whether to apply extra padding or not.
The relevant parts of the implementation are in the following three methods:
The full source code can be found here on GitHub.