Android中ListView嵌套ScrollView
这种情况下
ListView和其他的布局是平级的关系
都在ScrollView的布局之下
实现了滚动布局和列表
我们可以写一个SolidListView 继承 ListView
public class SolidListView extends ListView { public SolidListView(Context context) { super(context); } public SolidListView(Context context, AttributeSet attrs) { super(context, attrs); } public SolidListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
如果是GridView嵌套ScrollView
也是一样的道理
public class SolidGridView extends GridView { public SolidGridView(Context context) { super(context); } public SolidGridView(Context context, AttributeSet attrs) { super(context, attrs); } public SolidGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
在布局中,直接使用SolidListView和SolidGridView
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <com.lich.view.SolidListView android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.lich.view.SolidGridView android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>