问题描述
我在滚动型的一些看法。问题是,当我触摸子视图,并滚动它垂直,孩子认为只能获得 ACTION_DOWN
和 ACTION_CANCEL
事件。在 ACTION_MOVE
和 ACTION_UP
事件被错过。
如果我摸摸孩子视图和水平滚动它,孩子认为可以得到所有正如预期的触摸事件。
这里是我的code:
xml文件:
I have some views in ScrollView. The problem is when I touch the child view and scroll it vertically, the child view can only get ACTION_DOWN
and ACTION_CANCEL
event. The ACTION_MOVE
and ACTION_UP
event is missed.
If I touch the child view and scroll it horizontally, the child view can get all the touch event just as expected.
Here is my code:
xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/sv_test_tv"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="This is some text."
android:textSize="30sp" />
<View
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#faa" />
<View
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#faf" />
</LinearLayout>
</ScrollView>
Java的code:
java code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sv_test);
TextView tv = (TextView) findViewById(R.id.sv_test_tv);
tv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
LogUtils.v("ScrollViewTest", "onTouch:" + getType(event.getAction()));
return true;
}
});
}
private String getType(int type) {
switch (type) {
case MotionEvent.ACTION_DOWN: {
return "ACTION_DOWN";
}
case MotionEvent.ACTION_MOVE: {
return "ACTION_MOVE";
}
case MotionEvent.ACTION_UP: {
return "ACTION_UP";
}
}
return "other:" + type;
}
任何人都可以帮我吗?谢谢。
Anyone can help me? Thanks a lot.
推荐答案
您可以添加自定义滚动型到布局。
创建自定义的滚动视图,并在布局中使用它。
You can add custom scrollview to your layout.Create your custom scrollView and use it in your layout.
public class VerticalScrollview extends ScrollView{
public VerticalScrollview(Context context) {
super(context);
}
public VerticalScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
return false;
default: break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
//Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
return true;
}
}
添加&LT; packagename..VerticalScrollview /方式&gt;
到位滚动型
这篇关于滚动型的孩子不能得到ACTION_MOVE和ACTION_UP事件,当触摸儿童和垂直滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!