Tôi là lập trình viên n00b và cần rất nhiều trợ giúp.Android: vuốt màn hình để mở một hoạt động khác?
Chỉ cần cho mục đích hướng dẫn, tôi muốn làm cho hệ thực vật đơn giản & động vật (cây & động vật) Bách khoa toàn thư
Tôi muốn làm cho màn hình chủ của tôi kéo có thể giống như màn hình chủ của Android. Vuốt sang phải để mở trang Nhà máy và vuốt sang trái để mở trang Động vật. Tôi không biết cách tạo hiệu ứng chuyển tiếp . Vì vậy, chúng tôi có thể kéo nó nửa chừng để xem những gì ở trang tiếp theo và chỉ cần kéo lại để hủy bỏ nó
Các bạn có thể chia sẻ liên kết để tạo màn hình có thể kéo không?
Cảm ơn trước
[Chỉnh sửa]
@Agarwal Tôi đã thử các mã từ Link2 của bạn và nó không làm việc
tôi cố gắng để kiểm tra xem những cử chỉ phát hiện hay không bằng cách đặt Toast bên trong lớp bên trong nhưng Bánh mì nướng không được hiển thị. Link1 về cơ bản là giống nhau.
và từ vẻ của mã này, tôi nghĩ rằng nó không thể làm cho màn hình của tôi kéo có thể giống như trong màn hình chủ Android của
mã của tôi:
public class Home extends Activity implements OnClickListener {
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
ImageButton flora, fauna;
Intent go;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialize();
gestureDetector = new GestureDetector(new SwipeGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
}
private void initialize() {
//find view by id to image button
//set onClickListener to image button
}
public void onClick(View v) {
//normal switch and case for each button
}
private void onLeftSwipe() {
Toast t = Toast.makeText(Home.this, "Left swipe", Toast.LENGTH_LONG);
t.show();
go = new Intent("test.apps.FLORA");
startActivity(go);
}
private void onRightSwipe() {
Toast t = Toast.makeText(Home.this, "Right swipe", Toast.LENGTH_LONG);
t.show();
go = new Intent("test.apps.FAUNA");
startActivity(go);
}
private class SwipeGestureDetector extends 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 t = Toast.makeText(Home.this, "Gesture detected", Toast.LENGTH_SHORT);
t.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) {
Home.this.onLeftSwipe();
}
// Right swipe
else if (-diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Home.this.onRightSwipe();
}
} catch (Exception e) {
Log.e("Home", "Error on gestures");
}
return false;
}
}
}
Hey, câu hỏi nhanh chóng. Bạn đã bao giờ tìm ra lý do tại sao link2 không hoạt động? Tôi đang cố gắng làm cho nó hoạt động tốt. Nhưng bây giờ tôi không có may mắn như vậy. – Andy
Có, tôi đã tìm thấy mã nguồn tốt cho màn hình có thể kéo này, đây là liên kết http://marcreichelt.blogspot.sg/2010/09/android-use-realviewswitcher-to-switch.html?m=1 – hrsetyono
@DarcCode link bị hỏng, đây là liên kết thay thế [web.archive.org/web/2....](http://web.archive.org/web/20120728070120/http://marcreichelt.blogspot.sg/2010/09 /android-use-realviewswitcher-to-switch.html) – Olumide