2

我的问题:是否有在 FragmentActivity 中使用 onTouchEvent 的解决方法?

我的情况:我正在编写一个简单的手电筒应用程序,并且我有一个 FragmentActivity,用户可以在其中在颜色之间滑动(整个屏幕只是一种颜色。这个应用程序是为了个人改进)。我希望能够onLongPress为选项做一个事件,一个onDoubleTap隐藏导航栏的事件,最重要的是一个控制应用程序亮度的触摸事件(我想让用户上下滚动以改变亮度,这里有任何想法不客气!这个onFling活动有好处吗?)

我尝试过的:我有GestureDetector一个SimpleOnGestureListener. 我覆盖了onTouchEventin FragmentActivity,但我刚刚发现这个方法从来没有在 a 中调用过FragmentActivity(我也在Log.print方法中用 a 测试过它,它从来没有被调用过)。有解决办法吗?谢谢!

显着代码:

主要活动:

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
  }
}

主要的.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>

我认为您不需要其他任何东西来回答这个问题,但如果您需要,请告诉我。

4

3 回答 3

7

所以解决这个问题相对容易。问题是 ViewPager 正在吃触摸事件。所以我所做的是编写自己的 ViewPager 实现,并onTouchEvent(MotionEvent event)通过调用super.onTouchEvent(event)然后将事件传递给类中的gestureDetector 来覆盖该方法。

相关代码:

主要活动

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);
  }
}

我的手势监听器

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

<?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>
于 2012-11-01T15:48:39.880 回答
1

尝试这样做:

gestureDetector = new GestureDetector(this);

在 TouchEvent 上:

   @Override
   public boolean onTouchEvent(MotionEvent me) {
    return gestureDetector.onTouchEvent(me);
}

来源

于 2012-10-29T17:40:24.083 回答
1

如果您只想简单地检测用户何时触摸 fragmentActivity,请使用以下方法:

public void onUserInteraction(){
  Log.d("FragmentActivity","touched");
}

更多信息:http: //developer.android.com/reference/android/support/v4/app/FragmentActivity.html

于 2013-12-11T23:32:23.867 回答