<pre name="code" class="html">demo的xml布局
<RelativeLayout 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">
<ScrollView
android:id = "@+id/scrollview"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<com.example.scrollview.MyTextView
android:id ="@+id/textview"
android:layout_height="300dip"
android:layout_width="fill_parent"
android:text="触摸"
android:background="#ff0000"/>
<TextView
android:layout_height="700dip"
android:layout_width="fill_parent" />
<TextView
android:layout_height="700dip"
android:layout_width="fill_parent" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
//代码
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
//to do 逻辑...
//不想要父视图拦截触摸事件
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
getParent().requestDisallowInterceptTouchEvent(false);
break;
default:
break;
}
return true;
}
}