//////////////////////////////////////////////////// MainActivity /////////////////////////////////////////////////////////////////////////
public class MainActivity extends Activity {
Context context;private static ListView listView;
String[] names={"AA","BB","CC","DD","EE","1AA","1BB","1CC","1DD","1EE","2AA","2BB","2CC","2DD","2EE","3AA","3BB","3CC","3DD","3EE",};
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
init();
}
private void init() {
//找控件
final ScrollView scrollView=(ScrollView) findViewById(R.id.scrollView);
scrollView.smoothScrollTo(0, 20);
listView = (ListView) findViewById(R.id.listView);
//創建適配器
adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, names);
listView.setAdapter(adapter);
//給listView設置高度
setListViewHeightBasedOnChildren(listView);
//listView設置觸摸監聽
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){//觸摸擡起
//requestDisallowInterceptTouchEvent:請求不允許攔截觸摸事件
//scrollView攔截
scrollView.requestDisallowInterceptTouchEvent(false);
}else{
//按下或滑動時,scrollView不攔截
scrollView.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if((firstVisibleItem+visibleItemCount)==totalItemCount){
scrollView.requestDisallowInterceptTouchEvent(false);
}
}
});
}
/** 給ListView設置高度 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
if(listView == null) return;
//獲取ListView對應的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
//循環累計條目的高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
//條目的高度加上線寬
// params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
params.height = 500;
//把最終高度設置給listView
listView.setLayoutParams(params);
}
}
//////////////////////////////////////////////////// main_activity /////////////////////////////////////////////////////////////////////////
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大蘋果1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大蘋果2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大蘋果3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大蘋果4"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大蘋果5"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果6"
/>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果4"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果5"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果6"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果7"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果8"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小蘋果9"
/>
</LinearLayout>
</ScrollView>