java 如何使用 FragmentActivity 实现 onTouchEvent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13123129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement onTouchEvent with a FragmentActivity
提问by kentcdodds
My Question:Is there a work-around for using an onTouchEvent in a FragmentActivity?
我的问题:是否有在 FragmentActivity 中使用 onTouchEvent 的解决方法?
My Situation:I'm writing a simple flashlight app and I have a FragmentActivity where a user can swipe between colors (the whole screen is just one color. This app is for personal improvement). I want to be able to do a onLongPress
event for options, a onDoubleTap
event for hiding the navigation bar, and most importantlya touch event to control brightness of the app (I want to let the user scroll up and down for brightness changes, any ideas here are welcome! is the onFling
event good for this?)
我的情况:我正在编写一个简单的手电筒应用程序,我有一个 FragmentActivity,用户可以在其中滑动颜色(整个屏幕只有一种颜色。这个应用程序是为了个人改进)。我希望能够onLongPress
为选项做一个事件,一个onDoubleTap
隐藏导航栏的事件,最重要的是一个控制应用程序亮度的触摸事件(我想让用户上下滚动以改变亮度,这里有任何想法欢迎!这个onFling
活动对这个有好处吗?)
What I've tried:I have a GestureDetector
with my implementation of an SimpleOnGestureListener
. I override the onTouchEvent
in the FragmentActivity
, but I have just found out that this method is never called in a FragmentActivity
(I also tested this with a Log.print
in the method and it's never called). Is there a work-around? Thanks!
我尝试过的:我有GestureDetector
一个SimpleOnGestureListener
. 我覆盖了onTouchEvent
中的FragmentActivity
,但我刚刚发现从未在 a 中调用此方法FragmentActivity
(我也在Log.print
方法中使用 a进行了测试,但从未调用过)。有解决办法吗?谢谢!
Salient Code:
突出代码:
MainActivity:
主要活动:
public class MainActivity extends FragmentActivity {
private GestureDetector gestureDetector;
private FragmentPagerAdapter mAdapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(getApplicationContext(), new MyGestureListener(getWindow()));
mAdapter = new ColorFragmentAdapter(getSupportFragmentManager(), getDefaultColorList());
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(mAdapter);
pager.setCurrentItem(Math.round(mAdapter.getCount() / 2)); //Set current fragment to middle fragment
}
@Override
public boolean onTouchEvent(MotionEvent event) {
print("Sending touch event to gestureDetector.");
//This line is never run.
return gestureDetector.onTouchEvent(event);
}
/**
* Prints the given object to the log with the debug priority with the tag FLASHLIGHT
*
* @param object
*/
public static void print(Object object) {
Log.println(Log.DEBUG, "FLASHLIGHT", object.toString());
}
}
MyGestureListener :
我的手势监听器:
public class MyGestureListener extends SimpleOnGestureListener {
private final Window window;
public MyGestureListener(Window window) {
super();
this.window = window;
}
@Override
public void onLongPress(MotionEvent e) {
//onLongPress code
}
@Override
public boolean onDoubleTap(MotionEvent e) {
//onDoubleTap code
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//onFling cod
}
}
main.xml
主文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I don't think you'd need anything else to answer the question, but let me know if you do.
我认为你不需要其他任何东西来回答这个问题,但如果你需要,请告诉我。
回答by kentcdodds
So the solution to this is relatively easy. The problem is that the ViewPager is eating the touch event. So what I did was write my own implementation of a ViewPager and overrided the onTouchEvent(MotionEvent event)
method by calling super.onTouchEvent(event)
and then passing the event to the gestureDetector within the class.
所以解决这个问题相对容易。问题是 ViewPager 正在吃触摸事件。所以我所做的是编写自己的 ViewPager 实现,并onTouchEvent(MotionEvent event)
通过调用super.onTouchEvent(event)
然后将事件传递给类中的gestureDetector 来覆盖该方法。
Relevant Code:
相关代码:
MainActivity
主要活动
public class MainActivity extends FragmentActivity {
private FragmentPagerAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
MyViewPager pager = (MyViewPager) findViewById(R.id.viewpager);
pager.setGestureDetector(new GestureDetector(this, new MyGestureListener(this)));
pager.setAdapter(mAdapter);
pager.setCurrentItem(Math.round(mAdapter.getCount() / 2));
}
}
MyViewPager
我的浏览器
public class MyViewPager extends ViewPager {
private GestureDetector gestureDetector;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return super.onTouchEvent(e) && gestureDetector.onTouchEvent(e);
}
}
MyGestureListener
我的手势监听器
public class MyGestureListener extends SimpleOnGestureListener {
private final Activity activity;
public MyGestureListener(Activity activity) {
super();
this.activity = activity;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
//...
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
//...
}
//... etc...
}
layout_main.xml
布局_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.kentcdodds.flashlight.MyViewPager
android:id="@+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
回答by Heysus Escobar
回答by Basher51
If you just want to simply detect when a user touches on the fragmentActivity use the following method:
如果您只想简单地检测用户何时触摸 fragmentActivity,请使用以下方法:
public void onUserInteraction(){
Log.d("FragmentActivity","touched");
}
More info at : http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
更多信息:http: //developer.android.com/reference/android/support/v4/app/FragmentActivity.html