private int measureHeight() { // 获取ListView对应的Adapter ListAdapter adapter = mListView.getAdapter(); if (null == adapter) { return 0; } int totalHeight = 0; for (int i = 0, len = adapter.getCount(); i < len; i++) { View item = adapter.getView(i, null, mListView); if (null == item) continue; // 计算子项View 的宽高 item.measure(0, 0); // 统计所有子项的总高度 totalHeight += item.getMeasuredHeight(); } ViewGroup.LayoutParams params = mListView.getLayoutParams(); if (null == params) { params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } // 最后得到整个ListView完整显示需要的高度 params.height = totalHeight + (mListView.getDividerHeight() * (adapter.getCount() - 1)); mListView.setLayoutParams(params); return params.height; }
public void fixListViewHeight(ListView listView) { // 如果没有设置数据适配器,则ListView没有子项,返回。 ListAdapter listAdapter = listView.getAdapter(); int totalHeight = 0; if (listAdapter == null) { return; } for (int index = 0, len = listAdapter.getCount(); index < len; index++) { View listViewItem = listAdapter.getView(index, null, listView); // 计算子项View 的宽高 // listViewItem.measure(0, 0); listViewItem.measure( View.MeasureSpec.makeMeasureSpec( getResources().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY), 0); // 计算所有子项的高度和 int height = 0; if (listViewItem.getHeight() > listViewItem.getMeasuredHeight()) height = listViewItem.getHeight(); else height = listViewItem.getMeasuredHeight(); totalHeight += height; } ViewGroup.LayoutParams params = listView.getLayoutParams(); // listView.getDividerHeight()获取子项间分隔符的高度 // params.height设置ListView完全显示需要的高度 params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
public void userDogs(View view) { AlertDialog.Builder userDogsDialog = new AlertDialog.Builder(PublicUserProfileActivity.this); userDogsDialog.setTitle("User Dogs"); final ListView modeList = new ListView(PublicUserProfileActivity.this); modeList.setAdapter(adapter); modeList.setDividerHeight(modeList.getDividerHeight() * 3); userDogsDialog.setView(modeList); modeList.setOnItemClickListener( new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Dog dog = (Dog) modeList.getItemAtPosition(position); Intent clickedWalkProfile = new Intent(getApplicationContext(), PublicDogProfileActivity.class); clickedWalkProfile.putExtra("DOG", dog); startActivity(clickedWalkProfile); } }); AlertDialog alertDogs = userDogsDialog.create(); alertDogs.show(); }
public static void setListViewHeightBasedOnChildren(ListView listView, boolean mIsFlag) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; int moneHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 // sysout.println("----------高度----------"+listItem.getMeasuredHeight()); // if(i==len-1 && (mIsFlag)){ //评论特殊处理 // moneHeight = listItem.getMeasuredHeight()/2+10; // } } ViewGroup.LayoutParams params = listView.getLayoutParams(); totalHeight = moneHeight + totalHeight; params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // sysout.println("-----总高度----------------"+params.height); // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); // sysout.println("---------ListView高度----------"+listView.getLayoutParams().height); }
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 = 0; View view = null; for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) view.setLayoutParams( new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.MATCH_PARENT)); view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight() + 20; } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
/** * Sets ListView height dynamically based on the height of the items. * * @param listView to be resized * @return true if the listView is successfully resized, false otherwise */ 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); // Set list height. ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalItemsHeight + totalDividersHeight; listView.setLayoutParams(params); listView.requestLayout(); return true; } else { return false; } }
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = 0; listView.setLayoutParams(params); listView.requestLayout(); return; } int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom(); int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST); for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem != null) { listItem.setLayoutParams( new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
public void setListViewHeightBasedOnChildren(ListView listView, int offset) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 // listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); // listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST), // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + offset; // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mStickHeaderMap.isEmpty()) { return; } // measure header size int totalHeight = getPaddingTop(); int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); int size = mHeaderViews.size(); for (int i = 0; i < size; i++) { View view = mHeaderViews.get(i); view.measure(widthSpec, heightSpec); if (mStickHeaderMap.containsKey(view)) { StickView stickView = mStickHeaderMap.get(view); if (stickView.top == INVALID_TOP) { stickView.top = Math.min(totalHeight, getMeasuredHeight() - getPaddingBottom()); } ViewGroup.LayoutParams params = stickView.placeHolderView.getLayoutParams(); params.width = view.getMeasuredWidth(); params.height = view.getMeasuredHeight(); } totalHeight += (view.getMeasuredHeight() + mListView.getDividerHeight()); } }
public static void makeListViewFullSize(ListView lv, int itemHeight) { int itemCount = lv.getAdapter().getCount(); int divider = lv.getDividerHeight(); int height = (itemHeight + divider) * itemCount; ViewGroup.LayoutParams lp = lv.getLayoutParams(); lp.height = height; lv.setLayoutParams(lp); }
/** * get ListView height according to every children * * @param view * @return */ public static int getListViewHeightBasedOnChildren(ListView view) { int height = getAbsListViewHeightBasedOnChildren(view); ListAdapter adapter; int adapterCount; if (view != null && (adapter = view.getAdapter()) != null && (adapterCount = adapter.getCount()) > 0) { height += view.getDividerHeight() * (adapterCount - 1); } return height; }
private int getListViewHeight(ListView list) { ListAdapter adapter = list.getAdapter(); int listviewHeight = 0; list.measure( MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); listviewHeight = list.getMeasuredHeight() * adapter.getCount() + (adapter.getCount() * list.getDividerHeight()); return listviewHeight; }
public static void setListViewHeight(ListView mListView, BaseAdapter adapter) { adapter = (BaseAdapter) mListView.getAdapter(); if (adapter == null) { return; } int mTotalHeight = 0; for (int i = 0; i < adapter.getCount(); i++) { View mListItem = adapter.getView(i, null, mListView); mListItem.measure(0, 0); mTotalHeight += mListItem.getMeasuredHeight(); } ViewGroup.LayoutParams mParams = mListView.getLayoutParams(); mParams.height = mTotalHeight + (mListView.getDividerHeight() * (adapter.getCount() - 1)); mListView.setLayoutParams(mParams); }
/** * Populate Dynamic Listview * * @param listView */ public void setListView(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
/** 根据ListView的所有子项的高度设置其高度 */ public static void setListViewHeightByAllChildrenViewHeight(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter != null) { int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); listView.setLayoutParams(params); } }
/** * 计算listview的高度,但子ListView每个Item必须是LinearLayout * * @param listView */ public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight() + 5; } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; View listItem = null; for (int i = 0; i < listAdapter.getCount(); i++) { listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); ((MarginLayoutParams) params).setMargins(10, 10, 10, 10); listView.setLayoutParams(params); }
public static void getListViewSize(ListView myListView) { ListAdapter myListAdapter = myListView.getAdapter(); if (myListAdapter == null) { // do nothing return null return; } // set listAdapter in loop for getting final size int totalHeight = 0; for (int size = 0; size < myListAdapter.getCount(); size++) { View listItem = myListAdapter.getView(size, null, myListView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } // setting listview item in adapter ViewGroup.LayoutParams params = myListView.getLayoutParams(); params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1)); myListView.setLayoutParams(params); // print height of adapter on log Log.i("height of listItem:", String.valueOf(totalHeight)); }
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
public void setListViewHeightBasedOnChildren2( Context context, ListView listView, ListAdapter listAdapter) { // 获取ListView对应的Adapter if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + // MassVigUtil.dip2px(context, 10) + +(listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
public void justifyListViewHeightBasedOnChildren(ListView listView) { ListAdapter adapter = listView.getAdapter(); if (adapter == null) { return; } ViewGroup vg = listView; int totalHeight = 0; for (int i = 0; i < adapter.getCount(); i++) { View listItem = adapter.getView(i, null, vg); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams par = listView.getLayoutParams(); par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1)); listView.setLayoutParams(par); listView.requestLayout(); }
public static void setListViewHeightBasedOnChildren(ListView listView, BaseAdapter adapter) { // 获取ListView对应的Adapter if (adapter == null) { return; } int totalHeight = 0; for (int i = 0, len = adapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = adapter.getView(i, null, listView); // 计算子项View 的宽高 listItem.measure(0, 0); // 统计所有子项的总高度 totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1)); // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); }
private void configListViewHeight(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter != null) { int numberOfItems = listAdapter.getCount(); int totalItemsHeight = 0; for (int itemPos = 0; itemPos < numberOfItems; itemPos++) { View item = listAdapter.getView(itemPos, null, listView); item.measure(0, 0); totalItemsHeight += item.getMeasuredHeight(); } int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1); ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalItemsHeight + totalDividersHeight; listView.setLayoutParams(params); listView.requestLayout(); if (!Utility.sTwoPane) mTxtTitle.requestFocus(); } }
/* * 动态设置ListView组建的高度 */ public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // params.height += 5;// if without this statement,the listview will be // a // little short // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); System.out.println("高度" + totalHeight + "高度"); Log.v("height", totalHeight + ""); }
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int Size = 0; int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); Size = attachList.size(); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount()))) + listAdapter.getCount() + Size; listView.setLayoutParams(params); listView.requestLayout(); }
/** 设置ListView的高度(动态的) TODO delete */ private void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; if (listAdapter.getCount() > showItemCount) { for (int i = 0; i < showItemCount; i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } } else { for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); int count = 7; if (listAdapter.getCount() < 8) { count = listAdapter.getCount(); } if (totalHeight > 600) { totalHeight = 600; } params.height = totalHeight + (listView.getDividerHeight() * (count - 1)); // ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 可删除 listView.setLayoutParams(params); }
public static void setListViewHeight(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); int desiredWidth, resultHeight = 0; ViewGroup.LayoutParams params; View view = null; if (listAdapter == null) { return; } desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) { view.setLayoutParams( new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); } view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); resultHeight += view.getMeasuredHeight(); } params = listView.getLayoutParams(); params.height = resultHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
private void setSeparatorColor(String color) { int sepColor = TiColorHelper.parseColor(color); int dividerHeight = listView.getDividerHeight(); listView.setDivider(new ColorDrawable(sepColor)); listView.setDividerHeight(dividerHeight); }
@Override public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { int mappingCount = 0; if (attrs != null) { for (int i = 0, size = attrs.getAttributeCount(); i < size; ++i) { String value = attrs.getAttributeValue(i); if (!value.startsWith("?")) continue; int attrId = resources.getIdentifier( value.substring(1), null, null); // Integer.parseInt(value.substring(1)); if (attrId == 0) { Logger.e(TAG, "couldn't get id for attribute: " + value); continue; } int index = customAttrs.indexOfKey(attrId); if (index >= 0) { mappingKeys[mappingCount] = attrs.getAttributeNameResource(i); mappingValues[mappingCount] = customAttrs.valueAt(index); ++mappingCount; } } } if (mappingCount == 0 && textColorPrimaryOverridden == textColorPrimaryOriginal) return null; View view = instantiate(name, context, attrs); if (view == null) return null; boolean shouldOverrideTextColor = textColorPrimaryOverridden != textColorPrimaryOriginal && view instanceof TextView; for (int i = 0; i < mappingCount; ++i) { switch (mappingKeys[i]) { case android.R.attr.background: view.setBackgroundColor(mappingValues[i]); break; case android.R.attr.textColor: if (view instanceof TextView) { ((TextView) view).setTextColor(mappingValues[i]); shouldOverrideTextColor = false; } else { Logger.e( TAG, "couldn't apply attribute 'textColor' on class " + name + " (not instance of TextView)"); } break; case android.R.attr.divider: if (view instanceof ListView) { ListView listView = (ListView) view; int dividerHeight = listView.getDividerHeight(); listView.setDivider(new ColorDrawable(mappingValues[i])); listView.setDividerHeight(dividerHeight); } else { Logger.e( TAG, "couldn't apply attribute 'divider' on class " + name + " (not instance of ListView)"); } break; default: String attrResName = null; try { attrResName = resources.getResourceName(mappingKeys[i]); } catch (Exception e) { attrResName = Integer.toString(mappingKeys[i]); } Logger.e(TAG, "couldn't apply attribure '" + attrResName + "' on class " + name); } } if (shouldOverrideTextColor) { TextView tv = (TextView) view; if (tv.getCurrentTextColor() == textColorPrimaryOriginal) { tv.setTextColor(textColorPrimaryOverridden); } } return view; }
private int getFlHeight() { int listHeight = addresses.size() * ListItemHeight + (addresses.size() - 1) * lv.getDividerHeight(); return Math.min(MaxListHeight, Math.max(listHeight, MinListHeight)); }