检测ListActivity中的滑动手势

时间:2012-12-28 19:25:18

标签: android listview scrollview listactivity swipe

我搜索了很多关于我的问题,但我找不到答案。 我有一个ListActivity,它显示了我的sqlite数据库中的不同条目。 如果列表大于屏幕,它将自动变为可滚动。 我找到了一个检测滑动手势的教程,我实现了代码。 布局是ListView,它是android:id = "android:id/list",我还有一个空的View,它放在列表上,并将接收手势。 这个View在我的程序中称为v。 这是我的代码:

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;
        }

    }
}

如果我禁用了行v.setOnClickListener(ListViewActivity.this),我可以滚动并点击列表中的项目,但如果我激活此行,则会收到滑动手势,但我无法再滚动列表。

希望你们中的某个人有想法解决我的问题!

0 个答案:

没有答案