Nested GridView in ScrollView. The solution for displaying only one row in ListView is scrollviewgridview.
Reprinted: http://blog.csdn.net/luohai859/article/details/39347583
My personal understanding is: if you use the GridView and ListView separately, the content is fixed, and the underlying system can easily calculate the width and height occupied by the control, after a layer of ScrollView is nested on the outside, you can slide up or down or left or right, but you do not know how many systems can slide. In this case, you need to calculate a sliding area for ScrollView. The value is the sum of the total width and height of the content in the GridView and ListView. For more precise understanding, please kindly advise. There are two solutions:
Method 1: Calculate the height and display of the Child column in ListView or GridView:
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)); ((MarginLayoutParams)params).setMargins(15, 15, 15, 15); listView.setLayoutParams(params); }
Method 2: override the onMeasure method of the GridView and ListView, and directly give it a high enough height:
Override ListView:
public class MyListView extends ListView { public MyListView(Context context) { // TODO Auto-generated method stub super(context); } public MyListView(Context context, AttributeSet attrs) { // TODO Auto-generated method stub super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyle) { // TODO Auto-generated method stub super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
Rewrite the GridView:
public class MyGridView extends GridView{ public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }