GestureDetector绑定到一个TextView中的片段 [英] GestureDetector bound to a TextView in a fragment
问题描述
所以我有一个驻留在我的活动片段里面的TextView 。我想添加一个 gestureDetector 到的TextView ,以便用户可以进行,涉及到它的某些操作。我特别要轻扫或一扔。我的code是如下:
公共无效onViewCreated(查看视图,捆绑savedInstanceState)
{
myGestDetector =新GestureDetector(getActivity(),新GestureDetector.SimpleOnGestureListener());
TextView的mainTextView =(TextView中)getView()findViewById(R.id.HourCount)。
mainTextView.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(查看视图,MotionEvent motionEvent){
myGestDetector.onTouchEvent(motionEvent);
返回false;
} });}
@覆盖
公共布尔onDown(MotionEvent motionEvent){
返回false;
}@覆盖
公共无效昂秀preSS(MotionEvent motionEvent){}@覆盖
公共布尔onSingleTapUp(MotionEvent motionEvent){
返回false;
}@覆盖
公共布尔onScroll(MotionEvent motionEvent,MotionEvent motionEvent2,浮动V,V2浮动){
返回false;
}@覆盖
公共无效onLong preSS(MotionEvent motionEvent){}
@覆盖
公共布尔onFling(MotionEvent EVENT1,MotionEvent EVENT2,浮velocityX,浮velocityY){
Log.d(手势,onFling:+ event1.toString()+ event2.toString());
返回true;
}
问题是,我不能得到任何这些火灾。我试着做一扔了好几次,但日志不显示,我呼吁在 onFling 。没有人有任何想法,我做错了什么?
mainTextView.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(查看视图,MotionEvent motionEvent){
myGestDetector.onTouchEvent(motionEvent);
返回false; //从这里或更好的回报myGestDetector.onTouchEvent(motionEvent)返回true;
}});
通过返回true安卓知道你是消费触摸事件
请参阅: onFling为一个TextView不工作
编辑:
myGestDetector =新GestureDetector(getActivity(),新GestureListener());
做一个私有的类像这样
私有类GestureListener实现GestureDetector.OnGestureListener { 公共GestureListener(){
} @覆盖
公共布尔onDown(MotionEvent motionEvent){
返回false;
} @覆盖
公共无效昂秀preSS(MotionEvent motionEvent){ } @覆盖
公共布尔onSingleTapUp(MotionEvent motionEvent){
返回false;
} @覆盖
公共布尔onScroll(MotionEvent motionEvent,MotionEvent motionEvent2,浮动V,V2浮动){
返回false;
} @覆盖
公共无效onLong preSS(MotionEvent motionEvent){ }
@覆盖
公共布尔onFling(MotionEvent EVENT1,MotionEvent EVENT2,浮velocityX,浮velocityY){
Log.d(手势,onFling:+ event1.toString()+ event2.toString());
返回true;
}
}
So I have a TextView that resides inside a fragment in my activity. I'm trying to add a gestureDetector to that TextView so that the user can carry out certain actions that pertain to it. I especially want the swipe or fling. My code is as follows:
public void onViewCreated(View view, Bundle savedInstanceState)
{
myGestDetector = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener());
TextView mainTextView = (TextView)getView().findViewById(R.id.HourCount);
mainTextView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
myGestDetector.onTouchEvent(motionEvent);
return false;
}
});
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
Log.d("GESTURES", "onFling: " + event1.toString()+event2.toString());
return true;
}
The issue is that I cannot get any of these to fire. I've tried doing the fling several times, but the log isn't showing that I am calling the onFling. Does anyone have any idea what I'm doing wrong?
mainTextView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
myGestDetector.onTouchEvent(motionEvent);
return false; // Return true from here or better return myGestDetector.onTouchEvent(motionEvent);
}
});
By returning true android knows that you are consuming touch event Refer : onFling for a TextView not working
Edit :
myGestDetector = new GestureDetector(getActivity(), new GestureListener());
make a private class like this
private class GestureListener implements GestureDetector.OnGestureListener {
public GestureListener() {
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
Log.d("GESTURES", "onFling: " + event1.toString()+event2.toString());
return true;
}
}
这篇关于GestureDetector绑定到一个TextView中的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!