我有一個列表視圖。點擊某個項目後,該項目的詳細視圖將打開。這種佈局有許多小部件,如文本視圖,ImageView Buttons
等。現在我想滑動這些項目的詳細視圖來顯示列表中下一項的詳細視圖。同樣以前的項目爲從左到右。android:向左或向右滑動滑動視圖
我不能實現視圖滑動我已經完成了如何獲得列表中的前/後項目。但實際的滑動是問題
我試圖gesturedetector像Android: Swipe left to right and right to left
和其他一些例子。但是當我嘗試滑動時,沒有任何效果。我沒有看到任何視覺滑動。
如何解決這個問題?
嘗試這種代碼,但仍然沒有滑動發生
public class ProductActivity extends Activity implements OnGestureListener {
@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_details);
setContent();
}
private void setContent() {
Bundle extras = getIntent().getExtras();
TextView title = (TextView) findViewById(R.id.title);
title.setText(extras.getString("Title"));
TextView desc = (TextView) findViewById(R.id.desc);
desc.setText(extras.getString("Desc"));
Button buy = (Button) findViewById(R.id.buy);
String priceTag = ("$" + String.format("%.2g", extras.getDouble("Price")) + " Buy Now >>");
buy.setText(priceTag);
ImageView image = (ImageView) findViewById(R.id.productimage);
Utils.imageLoader.DisplayImage(extras.getString("Image"), image);
}
private static final int SWIPE_MIN_DISTANCE = 6; // 120;
private static final int SWIPE_MAX_OFF_PATH = 125; // 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 100; // 200;
private GestureDetector gestureScanner;
@
Override
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
// @Override
public boolean onDown(MotionEvent e) {
// viewA.setText("-" + "DOWN" + "-");
return true;
}
// @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Left Swipe",
Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Right Swipe",
Toast.LENGTH_SHORT).show();
} else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Swipe up",
Toast.LENGTH_SHORT).show();
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Swipe down",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return true;
}
@
Override
public void onLongPress(MotionEvent e) {
Toast mToast = Toast.makeText(getApplicationContext(), "Long Press",
Toast.LENGTH_SHORT);
mToast.show();
}
// @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// viewA.setText("-" + "SCROLL" + "-");
return true;
}
// @Override
public void onShowPress(MotionEvent e) {
// viewA.setText("-" + "SHOW PRESS" + "-");
} // @Override
public boolean onSingleTapUp(MotionEvent e) {
Toast mToast = Toast.makeText(getApplicationContext(), "Single Tap",
Toast.LENGTH_SHORT);
mToast.show();
return true;
}
}
我終於跟着這個鏈接:http://misha.beshkin.lv/android-swipe-gesture-implementation/。這正是我所期待的。感謝所有幫助 – png