package com.bwie.scrollview_lv_ori; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.View.OnTouchListener; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.ScrollView; public class MainActivity extends Activity { private ListView lv; private ScrollView sc; private String[] name = new String[] { "A", "S", "D", "F", "G", "H", "J","A1", "S1", "D1", "F1", "G1", "H1", "J1","A2", "S2", "D2", "F3", "G4", "H5", "J6", "H", "J", "K" }; private boolean isLoad=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找控件 lv = (ListView) findViewById(R.id.lv); sc = (ScrollView) findViewById(R.id.sc); // 为lv设置适配器 lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, name)); // 重新定义listiview的高度 setListViewHeightBasedOnChildren(lv); //为listview设置触摸事件 lv.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //请求父亲不要拦截 if(event.getAction() == 2) { sc.requestDisallowInterceptTouchEvent(true); }else{ sc.requestDisallowInterceptTouchEvent(false); } return false; } }); lv.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if((scrollState == SCROLL_STATE_TOUCH_SCROLL) && isLoad) { //滑到最低端时,就请求父亲可以拦截 sc.requestDisallowInterceptTouchEvent(false); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //判断lv是否加载到最低端 isLoad = (firstVisibleItem + visibleItemCount) == totalItemCount; } }); } private void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition 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 =500 /*totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))*/; listView.setLayoutParams(params); } }