下面列出了android.widget.ListView#getPaddingBottom ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public int setRecentUnlocksHeightBasedOnListView(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return (int) pxFromDp(550);
} else {
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
totalHeight += mCardTitle.getMeasuredHeight();
totalHeight += pxFromDp(16); // Add the paddings as well
totalHeight += pxFromDp(48); // Add the more items button
return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
}
}
public int setCriticalItemsHeightBasedOnListView(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return (int) pxFromDp(550);
} else {
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
totalHeight += mCardTitle.getMeasuredHeight();
totalHeight += pxFromDp(32); // Add the paddings as well
return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
}
}
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
int topPAdding = listView.getPaddingTop();
int bottomPadding = listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + topPAdding + bottomPadding;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
/**
* Set ListView height dynamically based on the height of the items.
*
* @param listView to be resized
* @see <a href="http://stackoverflow.com/questions/1778485/android-listview-display-all-available-items-without-scroll-with-static-header">stackoverflow answer</a>
*/
public static void SetListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
throw new RuntimeException("an adapter must be set before list view can be resized");
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);
//get vertical padding
int paddingVertical = listView.getPaddingTop() + listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + paddingVertical;
listView.setLayoutParams(params);
listView.requestLayout();
}
/**
* To save time measuring the height, this method gets an item if the height has not been
* previous measured and multiplies it by count of the total amount of items. It is fine if the
* height too small as the ListView will scroll through the other values.
* @param listView The ListView to measure the surrounding padding.
* @param listAdapter The adapter which contains the items within the list.
* @return Returns the combined height of the padding of the ListView and the approximate height
* of the ListView based off the an item.
*/
private int measureApproximateListViewHeight(
ListView listView, BaseAdapter listAdapter, int maxCount) {
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
if (mMenuItemHeight == 0 && !listAdapter.isEmpty()) {
View view = listAdapter.getView(0, null, listView);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mMenuItemHeight = view.getMeasuredHeight();
}
return totalHeight + mMenuItemHeight * maxCount;
}
/**
* This method adjusts the height of the given listView to match the combined height of all if its
* children and the dividers between list items. This is used to set the height of the mash step
* list such that it does not scroll, since it is encompassed by a ScrollView.
*
* @param listView
* ListView to adjust.
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
if (i == 0) {
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
//totalHeight += view.getMeasuredHeight();
}
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
boolean handlePostForDoubleClick(final View view) {
final PostViewHolder holder = ListViewUtils.getViewHolder(view, PostViewHolder.class);
if (holder != null) {
if (holder.comment.getVisibility() != View.VISIBLE || holder.comment.isSelectionEnabled()) {
return false;
}
long t = System.currentTimeMillis();
long timeout = holder.comment.getPreferredDoubleTapTimeout();
if (t - holder.lastCommentClick > timeout) {
holder.lastCommentClick = t;
} else {
final ListView listView = (ListView) view.getParent();
final int position = listView.getPositionForView(view);
holder.comment.startSelection();
int padding = holder.comment.getSelectionPadding();
if (padding > 0) {
final int listHeight = listView.getHeight() - listView.getPaddingTop() -
listView.getPaddingBottom();
listView.post(() -> {
int end = holder.comment.getSelectionEnd();
if (end >= 0) {
Layout layout = holder.comment.getLayout();
int line = layout.getLineForOffset(end);
int count = layout.getLineCount();
if (count - line <= 4) {
listView.setSelectionFromTop(position, listHeight - view.getHeight());
}
}
});
}
}
return true;
} else {
return false;
}
}
public static void updateListViewHeight(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null)
return;
// Get listView height
int totalHeight = myListView.getPaddingTop() + myListView.getPaddingBottom();
int adapterCount = myListAdapter.getCount();
for (int i = 0; i < adapterCount; i++) {
View listItem = myListAdapter.getView(i, null, myListView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// Change height of listView
ViewGroup.LayoutParams paramsList = myListView.getLayoutParams();
paramsList.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
myListView.setLayoutParams(paramsList);
}
@Override
public Bitmap capture(@NonNull ListView listView) {
List<View> viewList = new ArrayList<>();
try {
ListAdapter adapter = listView.getAdapter();
Drawable dividerDrawable = listView.getDivider();
Drawable backgroundDrawable = listView.getBackground();
int dividerHeight = listView.getDividerHeight();
int itemsCount = adapter.getCount();
int allHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int allWidth = listView.getMeasuredWidth() + listView.getPaddingLeft() + listView.getPaddingRight();
for (int i = 0; i < adapter.getCount(); i++) {
View childView = adapter.getView(i, null, listView);
childView.measure(
View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY),//
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
viewList.add(childView);
allHeight += childView.getMeasuredHeight();
}
allHeight += (itemsCount - 1) * dividerHeight;
Bitmap bigBitmap = Bitmap.createBitmap(allWidth, allHeight, Bitmap.Config.RGB_565);
Canvas bigCanvas = new Canvas(bigBitmap);
Paint paint = new Paint();
int iHeight = listView.getPaddingTop();
final Rect bounds = new Rect();
bounds.set(0, 0, allWidth, allHeight);
backgroundDrawable.setBounds(bounds);
backgroundDrawable.draw(bigCanvas);
for (int i = 0; i < viewList.size(); i++) {
View view = viewList.get(i);
Bitmap bmp = view.getDrawingCache();
bigCanvas.drawBitmap(bmp, listView.getPaddingLeft(), iHeight, paint);
iHeight += bmp.getHeight();
if (i < viewList.size() - 1 && dividerHeight > 0 && dividerDrawable != null) {
bounds.set(0, iHeight, allWidth, iHeight + dividerHeight);
dividerDrawable.setBounds(bounds);
dividerDrawable.draw(bigCanvas);
iHeight += dividerHeight;
}
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
bmp.recycle();
bmp = null;
}
report(bigBitmap);
return bigBitmap;
} finally {
viewList.clear();
viewList = null;
}
}