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>