在開發過程中難免會遇到ScrollView嵌套ListView的情況,對於這種情況有幾種比較好的解決方案。
一、當ListView的Item爲固定高度時,可以通過繼承ListView重寫onMeasure方法。
二、通過手動計算高度設置LayoutParams可以完美解決;
三、但當ListView的Item的高度不固定時,使用上面兩種方法會有一個bug,ListView的最後一個Item偶爾會出現顯示不全的問題,通過文中的第三種方法可以完美解決此問題。
ListView中的Item爲固定高度
一、可通過自定義ListView重寫onMeasure方法。
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
二、通過手動計算ListView的高度解決此問題。
注意這個方法只需要在setAdapter後調用。
//調用示範
ListView listView = (ListView) findViewById(id);
YourAdapter adapter = new MyAdapter("初始化你的適配器");
listView.setAdapter(adapter);
setListViewHeightBasedOnChildren(listView);
//自定義計算ListView高度方法
public static void setListViewHeightBasedOnChildren(ListView listView) {
//獲取ListView對應的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回數據項的數目
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); //計算子項View 的寬高
totalHeight += listItem.getMeasuredHeight(); //統計所有子項的總高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//listView.getDividerHeight()獲取子項間分隔符佔用的高度
//params.height最後得到整個ListView完整顯示需要的高度
listView.setLayoutParams(params);
}
但是這個方法有個兩個細節需要注意:
一是Adapter中getView方法返回的View的必須由LinearLayout組成,因爲只有LinearLayout纔有measure()方法,如果使用其他的佈局如RelativeLayout,在調用listItem.measure(0, 0);時就會拋異常,因爲除LinearLayout外的其他佈局的這個方法就是直接拋異常的。
二是使用這個方法的話,默認在ScrollView頂端的項是ListView。主要是因爲在界面在計算高度的時候最初並未計算ListView的高度,因此當我們自定義計算高度的時候便會出現這種情況。解決方法:
方法一:獲取ScrollView第一個控件的焦點保持最前
mTextView.setFocusable(true);//獲取焦點保持最前
mTextView.setFocusableInTouchMode(true);
mTextView.requestFocus();
方法二:直接使用ScrollView的smoothScrollTo方法
myScrollView.smoothScrollTo(0,20)
lv.setFocusable(false);//去掉listview的焦點
ListView中的Item高度不固定時
三丶當ListView的Item的高度不固定時,使用上面兩種方法會有一個bug,ListView的最後一個Item偶爾會出現顯示不全的問題,使用下面的類可以完美解決此問題。
public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
好了,總而言之各有個的優勢。我們可以根據我們自己的需求去選擇對應的方式,方法的使用在於靈活貫通,相信還有其他更好的方法我們可以去使用。因爲我始終相信 技術 永無止境。
參考資料鏈接:
http://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view
http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android