我用谷歌搜索了很多关于我的问题,但我找不到答案。我有一个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)
,我可以滚动并单击列表中的项目,但如果我激活此行,我会收到滑动手势,但我无法再滚动列表。
希望你们中的某个人有一个想法来解决我的问题!