我有一个显示产品的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;
}
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 :(得分: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
}