TextView 的 onFling 不起作用
我正在尝试检测 TextView 对象的 onFling 事件。下面的代码非常适合 onDown 事件,但无法获取 onScroll 或 onFling 事件。有什么指点吗?
谢谢
package rob.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity
{
private GestureDetector myGestDetector;
private TextView mainTextView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myGestDetector = new GestureDetector(this, new SimpleOnGestureListener()
{
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
mainTextView.setText("just scroll'n");
return false;
}
@Override
public boolean onDown(MotionEvent e1)
{
mainTextView.setText("on down");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
mainTextView.setText("on fling");
return false;
}
});
mainTextView = (TextView)findViewById(R.id.MainText);
mainTextView.setText("Starting app...");
mainTextView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
myGestDetector.onTouchEvent(event);
return false;
}
});
}
}
布局只是 LinearLayout 内的 TextView。
I'm trying to detect an onFling event for a TextView object. The code below works perfectly for the onDown event, however it fails to get the onScroll or onFling events. Any pointers?
Thanks
package rob.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity
{
private GestureDetector myGestDetector;
private TextView mainTextView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myGestDetector = new GestureDetector(this, new SimpleOnGestureListener()
{
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
mainTextView.setText("just scroll'n");
return false;
}
@Override
public boolean onDown(MotionEvent e1)
{
mainTextView.setText("on down");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
mainTextView.setText("on fling");
return false;
}
});
mainTextView = (TextView)findViewById(R.id.MainText);
mainTextView.setText("Starting app...");
mainTextView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
myGestDetector.onTouchEvent(event);
return false;
}
});
}
}
The layout is just a TextView inside a LinearLayout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你应该在这里返回 true 。您视图通过返回 false 来拒绝指针向下事件,并且因此不会收到任何后续事件。
I think you should return true here. You view rejects pointer down event by returning false and don't receive any subsequent events because of this.
但就我而言,它有效。我在 textView 的内部类中返回 true,但我确实从 TouchEvent 返回 false。
But in my case it works. I return true in innerclass of textView but i do return false from my TouchEvent.