android滑动到右侧和左侧gridView

android滑动到右侧和左侧gridView,android,gridview,swipe,swipe-gesture,Android,Gridview,Swipe,Swipe Gesture,我有一个显示产品的gridview,我想将产品更改为滑动,但我不识别移动。 我的类扩展BaseMenuActivity,BaseMenuActivity扩展Activity 我学了好几门教程,但我仍然无法掌握动作 public class GalleryProductActivity extends BaseMenuActivity{ public final String imageDirectory = Environment.getExternalStorageDirectory() +

我有一个显示产品的gridview,我想将产品更改为滑动,但我不识别移动。 我的类扩展BaseMenuActivity,BaseMenuActivity扩展Activity 我学了好几门教程,但我仍然无法掌握动作

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;

}

}你可以试试这个。创建一个类,该类扩展
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);
    }
}
然后在
活动中
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
    } 

我还缺少移动。您所拥有的将只使用GalleryProductItem填充GridView。这里没有关于刷卡的事。请在网站上搜索“onSwipe”或“ActivitySwipedTector”甚至“swipe”。
@Override
    public void onSwipe(Direction direction) {
        // handle cases here
    }