在ScrollView中使用ListView,ListView的高度會不正常。android
方式一:在XML中寫死 ide
android:layout_width="match_parent"
android:layout_height="120dp"
方式二:代碼中設置固定高度(若是在運行過程當中才能決定ListView高度)this
public void setHeight(int height){ LayoutParams params = this.listview.getLayoutParams(); params.width = LayoutParams.FILL_PARENT; params.height = height; listview.setLayoutParams(layoutParams); }
方式三:代碼中動態設置高度(讓ListView高度最大 顯示徹底全部數據)spa
public void setHeight(){ int height = 0; int count = adapter.getCount(); for(int i=0;i<count;i++){ View temp = adapter.getView(i,null,listview); temp.measure(0,0); height += temp.getMeasuredHeight(); } LayoutParams params = this.listview.getLayoutParams(); params.width = LayoutParams.FILL_PARENT; params.height = height; listview.setLayoutParams(layoutParams); }
前提1:ListView 顯示所有內容(假如4條數據 我顯示滿4條)code
上述方式三讓ListView高度最大,顯示徹底數據,滑動時就只會響應ScrollView滑動事件blog
此時ListView不能滑動,也不必滑動,由於已經顯示徹底數據。事件
前提1:ListView 不想顯示所有內容(假如100條數據 我只顯示10條 滑動查看其他)get
使用上述方式一或二,ListView沒有顯示徹底數據io
可是想焦點在ListView時滑動ListView,焦點在ScrollView時滑動ScrollViewclass
給ListView加上監聽OnTouchListener
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
scrollView.requestDisallowInterceptTouchEvent(true);
return false; } });