In android development, it is often encountered in the development of the interface of nesting ListView in ScrollView. Nesting ListView in ScrollView will bring several problems. After repeated practice, the perfect solution is summarized as follows:
1. Inherit ListView and overwrite its methods
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { / / Implement the listview to move in the scrollview int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); System.out.println("expandSpec = " + expandSpec); super.onMeasure(widthMeasureSpec, expandSpec); }
2. Let the ScrollView scroll to the top when the interface is initialized or the data is loaded:
ScrollView sv = (ScrollView) findViewById(R.id.main_scrollview); sv.smoothScrollTo(0, 0);