I have implemented a gesture detector for my listview items, where onfling gesture is detected, two buttons will be shown. The first few list items worked fine.
However when I scroll to the bottom of my listview, onfling will not work for the last item of my listview, hence two buttons are not shown. PS: I am still able to get the position of the last list item. I am currently implementing this in my ListFragment class.
What should I do to enable onFling to work on my last list item? Thank you for any help.
Edited with the code
public class ExampleFragment extends ListFragment{
protected GestureDetector gestureDetector;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gestureDetector = new GestureDetector(getActivity(), new MyGestureDetector());
getListView().setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
MotionEvent cancelEvent = MotionEvent.obtain(event);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
v.onTouchEvent(cancelEvent);
return true;
}
return false;
}
});
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int pos = getListView().pointToPosition((int)e1.getX(), (int)e1.getY());
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getActivity(), "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getActivity(), "Right Swipe", Toast.LENGTH_SHORT).show();
View v = getListView().getChildAt(pos);
if (v != null) {
Button edit = (Button) v.findViewById(R.id.edit);
Button connect = (Button) v.findViewById(R.id.connect);
if (edit.getVisibility() == View.GONE && connect.getVisibility() == View.GONE){
edit.setVisibility(View.VISIBLE);
connect.setVisibility(View.VISIBLE);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}