2015-04-21 220 views
1

我有一個顯示產品的gridview,我希望將產品更改爲滑動,但我不認識這些動作。 我的類擴展BaseMenuActivity和baseMenuActivity延伸活動 我採取了一些教程,但我還是無法管理運動android左右滑動gridView

public class GalleryProductActivity extends BaseMenuActivity{ 

public final String imageDirectory = Environment.getExternalStorageDirectory() + "/galaxyv2Img/"; 
private String nomenclature; 
private String activite; 
private String code; 
private GridView gridView; 
private GalleryProductAdapter adapter; 
private DatabaseHelper myDb; 
private SQLiteDatabase db; 
private int max,min; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    max = 10; 
    min = 0; 
    Intent intent = getIntent(); 
    nomenclature = intent.getStringExtra("nomenclature"); 
    activite = intent.getStringExtra("activite"); 
    code = intent.getStringExtra("code"); 

    adapter = new GalleryProductAdapter(this,getData()); 
    gridView = (GridView) findViewById(R.id.gridViewGallery); 
    gridView.setAdapter(adapter); 

    MyGestureListener listener = new MyGestureListener(GalleryProductActivity.this); 
    gridView.setOnTouchListener(listener); 
    listener.setSwipeGestureCallBack(this); 
} 

@Override 
public void onSwipe(String direction) { 
    // handle cases here 
} 

private ArrayList<GalleryProductItem> getData() { 
    final ArrayList<GalleryProductItem> imageItems = new ArrayList<>(); 
    myDb = new DatabaseHelper(this); 
    db = myDb.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(" SELECT reference,libelle FROM Produit WHERE nomenclature IS NOT NULL ANd rd IS NULL AND activite IS '"+code+"' AND nomenclature IS '"+nomenclature+"' ORDER BY id LIMIT "+min+","+max+";",null); 
    System.out.println(cursor.getCount()); 
    int cpt = 0; 
    if (cursor.moveToFirst()) { 
     do { 
      System.out.println(cursor.getString(0) +"\n"+ cursor.getString(1)); 
      imageItems.add(new GalleryProductItem("file:" +imageDirectory+cursor.getString(0)+".jpg",cursor.getString(1),cursor.getString(0))); 
      cpt += 1; 
     } while (cursor.moveToNext()); 
    } 
    return imageItems; 
} 

@Override 
public int getContentView() { 
    return R.layout.activity_product_gallery; 

} 

MyGestureListener:

public class MyGestureListener implements View.OnTouchListener { 
private GestureDetector detector; 
private SwipeGestureCallBack callBack; 

public MyGestureListener(Context context) { 
    detector = new GestureDetector(context, new MyGestureDetector()); 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    return detector.onTouchEvent(event); 
} 

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener { 
    private static final int SWIPE_MIN_DISTANCE = 20; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 100; 

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY() - e2.getY())) { 
      if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       callBack.onSwipe("LEFT"); 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       callBack.onSwipe("RIGHT"); 
      } 
     } 

     return false; 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 
} 

public void setSwipeGestureCallBack(GalleryProductActivity callBack) { 
    this.callBack = callBack; 
} 

public interface SwipeGestureCallBack { 
    public void onSwipe(String direction); 
} 

}

+0

我也失蹤運動。你擁有的只會使用GalleryProductItem填充GridView。這裏沒有關於刷卡的內容。請在網站上搜索「onSwipe」或「ActivitySwipeDetector」,甚至是「滑動」。 – SmulianJulian

回答

0

你可以試試這個。創建一個擴展GestureDetector.SimpleOnGestureListener的類來處理滑動手勢。

public class MyGestureListener implements View.OnTouchListener { 
    private GestureDetector detector; 
    private SwipeGestureCallBack callBack; 

    public MyGestureListener(Context context) { 
     detector = new GestureDetector(context, new MyGestureDetector()); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return detector.onTouchEvent(event); 
    } 

    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener { 
     private static final int SWIPE_MIN_DISTANCE = 20; 
     private static final int SWIPE_THRESHOLD_VELOCITY = 100; 

     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY() - e2.getY())) { 
       if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
         && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
        callBack.onSwipe(Direction.LEFT); 
       } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
         && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
        callBack.onSwipe(Direction.RIGHT); 
       } 
      } 

      return false; 
     } 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 
    } 

    public void setSwipeGestureCallBack(SwipeGestureCallBack callBack) { 
     this.callBack = callBack; 
    } 

    public interface SwipeGestureCallBack { 
     public void onSwipe(Direction direction); 
    } 
} 

然後在你的Activity設置你的GridView的onTouchListener到MyGestureListener

MyGestureListener listener = new MyGestureListener(MyActivity.this); 
myGridView.setOnTouchListener(listener); 
listener.setSwipeGestureCallBack(this); 

最後一個實例,處理您的活動的回調通過實施SwipeGestureCallBack

@Override 
    public void onSwipe(Direction direction) { 
     // handle cases here 
    }