我想在不使用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 :(得分: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);