1

I googled a lot about my problem but I couldn't find the answer. I have a ListActivity which shows different entries from my sqlite database. If the list is bigger than the screen, it automatically becomes scrollable. I found a tutorial for detecting a swipe gesture and I implemented the code. The layout is the ListView which's android:id = "android:id/list" and I have also got an empty View which is placed over the list and which is going to receive the gestures. This View is called v in my program. Here's my code:

public class ListViewActivity extends ListActivity implements OnClickListener {

    // Cursor
    private SimpleCursorAdapter adapter;
    private Cursor cursor;

    // Database
    private DBAccess dbAccess;

    // Gesture
    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;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);

        /* ... */

        // Gesture detection
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                Log.v("ListViewActivity", "onTouch");
                return gestureDetector.onTouchEvent(event);
            }
        };

        View v = (View) findViewById(R.id.listview_view1);
        //v.setOnClickListener(ListViewActivity.this); 
        v.setOnTouchListener(gestureListener);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {     
        Cursor item = (Cursor) getListAdapter().getItem(position);

        Log.v("onListItemClick(...)", String.valueOf(id));

        Intent intent = new Intent(ListViewActivity.this, ListClickActivity.class);
        intent.putExtra("id", item.getString(0));
        startActivity(intent);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.v("ListViewActivity", "onClick");
        //Filter f = (Filter) v.getTag();
        //FilterFullscreenActivity.show(this, input, f);
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.v("ListViewActivity", "onFling");
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.v("onFling", "Left Swipe");
                    Toast.makeText(ListViewActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.v("onFling", "Right Swipe");
                    Toast.makeText(ListViewActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

    }
}

If I disable the line v.setOnClickListener(ListViewActivity.this) I can scroll and click on my items in the list, but if I activate this line I receive swipe gestures, but I can't scroll the list anymore.

Hopefully someone of you has an idea to solve my problem!

medphys
  • 1,157
  • 3
  • 10
  • 15

0 Answers0