Based on the following code snippets, I was wondering why the onFling gesture doesn't get recognized for a GridView of Buttons (I use Buttons instead of other Views for personal reasons):
Here's my MainActivity:
public class MainActivity extends AppCompatActivity {
private GridView grid;
GestureDetector gDetector;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView)findViewById(R.id.grid);
// 4X4 grid.
grid.setNumColumns(4);
ArrayList<Button> mButtons = new ArrayList<Button>();
Button button = null;
for (int i = 0; i < 16; i++) {
button = new Button(this);
button.setText(i + "");
mButtons.add(button);
}
grid.setAdapter(new CustomAdapter(this, mButtons));
gDetector = new GestureDetector(this, new SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent event) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final int position = grid.pointToPosition(Math.round(e1.getX()), Math.round(e1.getY()));
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH
|| Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
return false;
}
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this, "top at index " + position, Toast.LENGTH_SHORT).show();
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this, "bottom at index " + position, Toast.LENGTH_SHORT).show();
}
} else {
if (Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
return false;
}
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this, "left at index " + position, Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this, "right at index " + position, Toast.LENGTH_SHORT).show();
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gDetector.onTouchEvent(event);
}
... here's my CustomAdapter:
public class CustomAdapter extends BaseAdapter {
private ArrayList<Button> mButtons = null;
private Context ctx;
public CustomAdapter(Context ctx, ArrayList<Button> button) {
this.ctx = ctx;
this.mButtons = button;
}
@Override
public int getCount() {
return mButtons.size();
}
@Override
public Object getItem(int position) {return (Object) mButtons.get(position);}
@Override
public long getItemId(int position) {
// Position and id are synonymous.
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button button;
// Assigns a view to convertView should it be null, otherwise, casts convertView to the
// correct View type.
if (convertView == null) {
button = mButtons.get(position);
} else {
button = (Button) convertView;
}
return button;
}
}
... and apparently the swipe, onFling, would only get recognized on the bottom half of the screen when GridView is set to wrap_content, whereas the swipe wouldn't work at all when GridView is set to match_parent. Here's the grid set at wrap_content with the swipe only working in the enclosed square as follows:
Much appreciated.