//建立Scrollview类
public class MyScrollView extends ScrollView {
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
//建立listview继承listview
package com.example.scrollview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
public class Mylistview extends ListView {
int mLastMotionY;
boolean bottomFlag;
public Mylistview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//对height重新赋值
heightMeasureSpec= MeasureSpec.makeMeasureSpec(/*Integer.MAX_VALUE>> 2*/200,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/*@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return true;
}*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if (bottomFlag) {
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
int y = (int) ev.getRawY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
int deltaY = y - mLastMotionY;
if (deltaY < 0) {
View child = getChildAt(0);
if (child != null) {
if (getLastVisiblePosition() == (getChildCount()-1) && child.getBottom() == (getChildCount()-1)) {
bottomFlag = true;
getParent().requestDisallowInterceptTouchEvent(true);
}
int bottom = child.getBottom();
int padding = getPaddingTop();
if (getLastVisiblePosition() == (getChildCount()-1)
&& Math.abs(bottom - padding) >= 20) {
bottomFlag = true;
getParent().requestDisallowInterceptTouchEvent(true);
}
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return super.onTouchEvent(ev);
}
public void setBottomFlag(boolean flag) {
bottomFlag = flag;
}
}
//XML文件
//复制Scrollview类
<com.example.scrollview.MyScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//复制listview类
<com.example.scrollview.Mylistview
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="100sp" >
</com.example.scrollview.Mylistview>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中国" />
</LinearLayout>
</com.example.scrollview.MyScrollView>