我的问题是:在碎片活动中使用ontouchevent是否有解决方法?
我的情况:我正在写一个简单的手电筒应用程序,我有一个片段活动,用户可以在不同颜色之间滑动(整个屏幕只有一种颜色)。此应用程序用于个人改进)。我希望能够为选项做一个onLongPress
事件,一个用于隐藏导航栏的onDoubleTap
事件,最重要的是一个触摸事件来控制应用程序的亮度(我想让用户上下滚动来改变亮度,欢迎任何想法!onFling
事件对此有好处吗?)
我已经尝试过了:我有一个我实现了一个GestureDetector
。我重写了SimpleOnGestureListener
中的onTouchEvent
,但我刚刚发现,在FragmentActivity
中从未调用过此方法(我还使用方法中的FragmentActivity
测试了此方法,但从未调用过它)。有工作吗?谢谢!
显著代码:
主要活动:
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());
}
}
我的手势侦听器:
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
}
}
MIN
<?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>
我认为你不需要别的东西来回答这个问题,但如果你需要的话,请告诉我。
最佳答案
所以解决这个问题相对容易。问题是viewpager正在吃掉touch事件。因此,我所做的是编写自己的viewpager实现,并通过调用onTouchEvent(MotionEvent event)
重写super.onTouchEvent(event)
方法,然后将事件传递给类中的gesturedector。
相关代码:
主要活动
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));
}
}
小精灵
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);
}
}
我的手势听者
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...
}
布局主要.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>
关于java - 如何使用FragmentActivity实现onTouchEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13123129/