Android 如何测量子可扩展列表的高度和宽度
我正在使用可扩展列表视图制作3级层次结构,想知道如何设置内部列表的高度和宽度。 我知道我们有一个度量,但在我的例子中,它不允许我捕获父列表视图的整个空间 可能我给它的值不对,下面是我用来设置子可扩展列表的高度和宽度的代码Android 如何测量子可扩展列表的高度和宽度,android,android-layout,expandablelistview,Android,Android Layout,Expandablelistview,我正在使用可扩展列表视图制作3级层次结构,想知道如何设置内部列表的高度和宽度。 我知道我们有一个度量,但在我的例子中,它不允许我捕获父列表视图的整个空间 可能我给它的值不对,下面是我用来设置子可扩展列表的高度和宽度的代码 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { widthMeasureSpec = MeasureSpec.makeMeasureSpec(9
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(800,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
目前显示如下
它应该如下所示。
请提出同样的建议
谢谢您的时间。为ParentGroup和ChildParentGroup创建一个布局xml文件,为Child创建另一个布局xml文件。现在你们知道了,问题被简化为两个层次。然后在可展开的listview中,我们有父视图和子视图方法来展开和使用父布局和子布局。因此,在这种方法中,您可以做任何您想做的事情。为ParentGroup和ChildParentGroup创建一个布局xml文件,为Child创建另一个布局xml文件。现在你们知道了,问题被简化为两个层次。然后在可展开的listview中,我们有父视图和子视图方法来展开和使用父布局和子布局。因此,在这种方法中,您可以做任何您想做的事情。不确定您是否仍在寻找答案,但我就是这样做的:在构造函数中传递对父视图的引用和高度度量(在本例中,我使用子列表的大小)来创建子自定义列表编辑:或者为了使代码更加一致,可以将父视图的宽度和高度度量值传递给构造函数,而不是传递父视图本身。public CustomExpandableList(Context context, View the_parentView, int the_heightMeasure) { super(context); WIDTH = the_parentView!=null?the_parentView.getWidth():LayoutParams.MATCH_PARENT; HEIGHT = the_heightMeasure * 500; }
CustomExpandableList(Context-the-the-Context,int-the-width,int-heightMeasure)不确定您是否仍在寻找答案,但我就是这样做的:在构造函数中传递对父视图的引用和高度度量(在本例中,我使用了子列表的大小)来创建子自定义列表编辑:或者为了使代码更加一致,可以将父视图的宽度和高度度量值传递给构造函数,而不是传递父视图本身。public CustomExpandableList(Context context, View the_parentView, int the_heightMeasure) { super(context); WIDTH = the_parentView!=null?the_parentView.getWidth():LayoutParams.MATCH_PARENT; HEIGHT = the_heightMeasure * 500; }
CustomExpandableList(上下文-上下文-上下文,int-宽度,int-高度度量)使用此代码动态计算可扩展列表视图:// calculate the height of expandable listView without expanded private void setListViewHeight(ExpandableListView expListView) { ListAdapter listAdapter = expListView.getAdapter(); int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, expListView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); System.out.println("i " + i); } ViewGroup.LayoutParams params = expListView.getLayoutParams(); params.height = totalHeight + (expListView.getDividerHeight() * (listAdapter.getCount() - 1)); System.out.println("params.height = " + params.height); expListView.setLayoutParams(params); expListView.requestLayout(); } // calculate the height of expandable listview dynamically private void setListViewHeight(ExpandableListView expListView, int group) { ExpandableListAdapter listAdapter = expListView .getExpandableListAdapter(); int totalHeight = 0; int desiredWidth = MeasureSpec.makeMeasureSpec(expListView.getWidth(), MeasureSpec.UNSPECIFIED); for (int i = 0; i < listAdapter.getGroupCount(); i++) { View groupItem = listAdapter.getGroupView(i, false, null, expListView); groupItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); totalHeight += groupItem.getMeasuredHeight(); if (((expListView.isGroupExpanded(i)) && (i == group)) || ((!expListView.isGroupExpanded(i)) && (i == group))) { for (int j = 0; j < listAdapter.getChildrenCount(i); j++) { View listItem = listAdapter.getChildView(i, j, false, null, expListView); Log.e("Count", listAdapter.getChildrenCount(i) + ""); listItem.setLayoutParams(new ViewGroup.LayoutParams( desiredWidth, MeasureSpec.UNSPECIFIED)); // listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec .makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); totalHeight += listItem.getMeasuredHeight(); System.out.println("totalHeight" + totalHeight); Log.e("TEST", "gshdkfmjfy,"); } } } ViewGroup.LayoutParams params = expListView.getLayoutParams(); int height = totalHeight + (expListView.getDividerHeight() * (listAdapter .getGroupCount() - 1)); if (height < 10) { height = 100; } params.height = height; expListView.setLayoutParams(params); expListView.requestLayout(); }
使用此代码动态计算可扩展列表视图://计算未展开的可展开listView的高度 私有void setListViewHeight(ExpandableListView expListView){ ListAdapter ListAdapter=expListView.getAdapter(); int totalHeight=0; 对于(int i=0;i
// calculate the height of expandable listView without expanded private void setListViewHeight(ExpandableListView expListView) { ListAdapter listAdapter = expListView.getAdapter(); int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, expListView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); System.out.println("i " + i); } ViewGroup.LayoutParams params = expListView.getLayoutParams(); params.height = totalHeight + (expListView.getDividerHeight() * (listAdapter.getCount() - 1)); System.out.println("params.height = " + params.height); expListView.setLayoutParams(params); expListView.requestLayout(); } // calculate the height of expandable listview dynamically private void setListViewHeight(ExpandableListView expListView, int group) { ExpandableListAdapter listAdapter = expListView .getExpandableListAdapter(); int totalHeight = 0; int desiredWidth = MeasureSpec.makeMeasureSpec(expListView.getWidth(), MeasureSpec.UNSPECIFIED); for (int i = 0; i < listAdapter.getGroupCount(); i++) { View groupItem = listAdapter.getGroupView(i, false, null, expListView); groupItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); totalHeight += groupItem.getMeasuredHeight(); if (((expListView.isGroupExpanded(i)) && (i == group)) || ((!expListView.isGroupExpanded(i)) && (i == group))) { for (int j = 0; j < listAdapter.getChildrenCount(i); j++) { View listItem = listAdapter.getChildView(i, j, false, null, expListView); Log.e("Count", listAdapter.getChildrenCount(i) + ""); listItem.setLayoutParams(new ViewGroup.LayoutParams( desiredWidth, MeasureSpec.UNSPECIFIED)); // listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec .makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); totalHeight += listItem.getMeasuredHeight(); System.out.println("totalHeight" + totalHeight); Log.e("TEST", "gshdkfmjfy,"); } } } ViewGroup.LayoutParams params = expListView.getLayoutParams(); int height = totalHeight + (expListView.getDividerHeight() * (listAdapter .getGroupCount() - 1)); if (height < 10) { height = 100; } params.height = height; expListView.setLayoutParams(params); expListView.requestLayout(); }
//计算未展开的可展开listView的高度 私有void setListViewHeight(ExpandableListView expListView){ ListAdapter ListAdapter=expListView.getAdapter(); int totalHeight=0; 对于(int i=0;i
public static void setExpandableListViewHeightBasedOnChildren(ExpandableListView expandableListView){ ExpandableNotesListAdapter adapter = (ExpandableNotesListAdapter) expandableListView.getExpandableListAdapter(); if (adapter == null){ return; } int totalHeight = expandableListView.getPaddingTop() + expandableListView.getPaddingBottom(); for (int i = 0 ; i < adapter.getGroupCount() ; i++){ View groupItem = adapter.getGroupView(i, false, null, expandableListView); groupItem.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED); totalHeight += groupItem.getMeasuredHeight(); if (expandableListView.isGroupExpanded(i) ){ for( int j = 0 ; j < adapter.getChildrenCount(i) ; j++) { View listItem = adapter.getChildView(i, j, false, null, expandableListView); listItem.setLayoutParams(new LayoutParams(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)); listItem.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); totalHeight += listItem.getMeasuredHeight(); } } } ViewGroup.LayoutParams params = expandableListView.getLayoutParams(); int height = totalHeight + expandableListView.getDividerHeight() * (adapter.getGroupCount() - 1); if (height < 10) height = 100; params.height = height; expandableListView.setLayoutParams(params); expandableListView.requestLayout(); }
Functions.setExpandableListViewHeightBasedOnChildren(listView); listView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { Functions.setExpandableListViewHeightBasedOnChildren(listView); } });
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
public class CustomExpListView extends ExpandableListView{ public CustomExpListView(Context context){ super(context); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.EXACTLY); heightMeasureSpec = MeasureSpec.makeMeasureSpec(20000, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
private fun getSubItemTotalHeight(groupPosition: Int): Int { val children: Int = mAdapter.getChildrenCount(groupPosition) val desiredWidth: Int = View.MeasureSpec.makeMeasureSpec(mExpandableListView.width, View.MeasureSpec.UNSPECIFIED) var subItemTotalHeight = 0 repeat(children) { val child = mAdapter.getChildView(groupPosition, it, true, null, null) child.layoutParams = ViewGroup.LayoutParams( desiredWidth, View.MeasureSpec.UNSPECIFIED) child.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) subItemTotalHeight += child.measuredHeight } val dividerCount = children - 1 val dividerTotalCount = (dividerCount * mExpandableListView.dividerHeight).toFloat() showToast(mExpandableListView.dividerHeight.toString()) val totalDividerPixels = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dividerTotalCount, resources.displayMetrics ) return subItemTotalHeight + totalDividerPixels.toInt() }
mExpandableListView.setOnGroupExpandListener { groupPosition -> mExpandableListView.layoutParams.height += getSubItemTotalHeight(groupPosition) mExpandableListView.requestLayout() } mExpandableListView.setOnGroupCollapseListener { groupPosition -> mExpandableListView.layoutParams.height -= getSubItemTotalHeight(groupPosition) mExpandableListView.requestLayout() }