2016-06-07 65 views
0

我想从一个活动滑动到另一个没有使用Viewpager,我只想使用SwipeGesture我已经写了代码,但我的屏幕不滑动,所以任何人都可以告诉我什么是错的代码?这里是我的代码:手势检测器不工作

public class Ashtakoota extends AbstractActivity implements IAPICallback,View.OnClickListener { 


private LinearLayout lnrAsht; 

private GestureDetector gestureDetector; 
View.OnTouchListener gestureListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getLayoutInflater().inflate(R.layout.activity_ashtakoota,mFrameLayout); 


    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); 
    mDrawerToggle.setDrawerIndicatorEnabled(false); 
    lnrAsht = (LinearLayout)findViewById(R.id.lnrAsht); 

    mbtnBack = (FancyButton)findViewById(R.id.btn_match_ashtakootback); 
    mbtnNext =(FancyButton)findViewById(R.id.btn_match_ashtakootnext); 

    mbtnBack.setOnClickListener(this); 
    mbtnNext.setOnClickListener(this); 
    lnrAsht.setOnTouchListener(gestureListener); 

    gestureDetector = new GestureDetector(new SwipeGestureDetector()); 
    gestureListener = new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      return gestureDetector.onTouchEvent(event); 
     } 
    }; 




    mHandler = new ViewHandler(); 
    callAPI(); 





} 



private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener { 

    private static final int SWIPE_MIN_DISTANCE = 50; 
    private static final int SWIPE_MAX_OFF_PATH = 200; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
          float velocityY) { 
     try { 
      Toast.makeText(Ashtakoota.this, "Gesture Detected", Toast.LENGTH_SHORT).show(); 
      float diffAbs = Math.abs(e1.getY() - e2.getY()); 
      float diff = e1.getX() - e2.getX(); 

      if (diffAbs > SWIPE_MAX_OFF_PATH) 
       return false; 

      // Left swipe 
      if (diff > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       onLeftSwipe(); 
      } 
      // Right swipe 
      else if (-diff > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       onRightSwipe(); 
      } 
     } catch (Exception e) { 
      Log.e("Home", "Error on gestures"); 
     } 
     return false; 
    } 


} 

private void onLeftSwipe() { 
    Intent intent = new Intent(this,BasicAstro.class); 
    startActivity(intent); 
} 

private void onRightSwipe() { 
    Intent intent = new Intent(this,MatchManglik.class); 
    startActivity(intent); 
} 


@Override 
public void onClick(View v) { 
    int id = v.getId(); 
    switch (id) { 
     case R.id.btn_match_ashtakootback:{ 
      Intent intent = new Intent(this,BasicAstro.class); 
      startActivity(intent);}break; 

     case R.id.btn_match_ashtakootnext:{ 
      Intent intent = new Intent(this,MatchManglik.class); 
      startActivity(intent);}break; 
    } 
} 

} 

}

回答

0

你初始化之前设置的动作监听器。

试试这个:

gestureDetector = new GestureDetector(new SwipeGestureDetector()); 
gestureListener = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return gestureDetector.onTouchEvent(event); 
    } 
}; 

lnrAsht.setOnTouchListener(gestureListener); 
+0

我试过,但它不工作! – bipin

+0

@agunal你能帮忙吗? – bipin

+0

@Mirek Rusin你能帮忙吗? – bipin