作者:树上的女爷 | 来源:互联网 | 2023-06-15 19:36
直接上代码来的直接点吧……packagecom.zhuo.scrollview_listview;importjava.util.ArrayList;import
直接上代码来的直接点吧……
package com.zhuo.scrollview_listview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
public class MainActivity extends Activity {
private ListView listView;
private ScrollView scrollView;
private ArrayAdapter adapter;
/**
* 标记:ListView中的Item是否滑动到底端.
*/
private boolean isListViewMoveToBottom = false;
/**
* 表示是否可以滑动ScrollView.(当 isListViewMoveToBottom = true,分发事件给它滑动ScrollView.)
*/
private boolean isScrollViewMoved = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) this.findViewById(R.id.scrollView);
listView = (ListView) this.findViewById(R.id.listview);
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, getData());
listView.setAdapter(adapter);
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 表示用户将ListView中的Item滑动到底端.
switch (scrollState) {
// the user is scrolling using touch,and their finger is still
// on the screen.
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;
// the view is not scrolling .
case 0:
if (isListViewMoveToBottom) {
// 表示ScrollView可以获取滑动事件.
isScrollViewMoved = true;
} else {
isScrollViewMoved = false;
}
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
// 表示ListView中所有Item已经滑动到ListView的底端.(在向同一个方向滑动,则应该ScrollView滑动.)
isListViewMoveToBottom = true;
} else {
isListViewMoveToBottom = false;
}
}
});
}
private ArrayList getData() {
ArrayList dataArrayList = new ArrayList();
for (int i = 0; i <30; i++) {
dataArrayList.add("xxx:" + i);
}
return dataArrayList;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_MOVE) {
// 想让哪个控件先执行,直接调用。
if (isScrollViewMoved) {
// 将滑动事件分发给ScrollView.
scrollView.dispatchTouchEvent(ev);
// 释放到ScrollView获取的事件.(否则,会覆盖其子控件View的相同事件.)
isScrollViewMoved = false;
} else {
// 分发事件给ListView,让点击ListView的时候,其可以滑动。
listView.dispatchTouchEvent(ev);
}
}
return super.dispatchTouchEvent(ev);
}
}
布局文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:cOntext="com.zhuo.scrollview_listview.MainActivity" >
android:id="@+id/scrollView"
android:layout_
android:layout_
android:layout_alignParentTop="true" >
android:id="@+id/linear"
android:layout_
android:layout_
android:orientation="vertical" >
android:id="@+id/listview"
android:layout_
android:layout_ />