好的,我有一个ListView,工作正常,从数据库获取数据。 想要做ol'在ListView中向左滑动以删除项目。
我已实施OnSwipeTouchListener
- 来自https://stackoverflow.com/a/19506010/2446010
工作正常。一切都很开心。
唯一问题 - getItem(position)
- 如何获取我已刷过的项目的位置以说删除它?
mNameListView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
WorkoutExercises workoutExercises = mWorkoutExercisesAdapter.getItem(position);
workoutExercises.deleteInBackground();
Toast.makeText(getApplicationContext(), "Deleted from " + mWorkoutNameDisplay, Toast.LENGTH_LONG).show();
getCurrentExercisesInWorkout();
}
});
我现在所做的是把它放在OnItemClick
里面,它以一个int位置作为参数。
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
当点击时它起作用并传递位置 - 当然只有问题我必须先点击然后向左滑动。如何通过/存储我刷过的位置以传递给onSwipeLeft
?
谢谢堆!
答案 0 :(得分:1)
我做了一些研究,并查看了StackOverflow上的其他一些示例。
我实现了一个滑动检测器类:
public class SwipeDetector implements View.OnTouchListener {
public static enum Action {
LR, // Left to Right
RL, // Right to Left
TB, // Top to bottom
BT, // Bottom to Top
None // when no action was detected
}
private static final String logTag = "SwipeDetector";
private static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;
public boolean swipeDetected() {
return mSwipeDetected != Action.None;
}
public Action getAction() {
return mSwipeDetected;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.None;
return false; // allow other events like Click to be processed
case MotionEvent.ACTION_UP:
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
Log.i(logTag, "Swipe Left to Right");
mSwipeDetected = Action.LR;
return false;
}
if (deltaX > 0) {
Log.i(logTag, "Swipe Right to Left");
mSwipeDetected = Action.RL;
return false;
}
} else if (Math.abs(deltaY) > MIN_DISTANCE) { // vertical swipe
// detection
// top or down
if (deltaY < 0) {
Log.i(logTag, "Swipe Top to Bottom");
mSwipeDetected = Action.TB;
return false;
}
if (deltaY > 0) {
Log.i(logTag, "Swipe Bottom to Top");
mSwipeDetected = Action.BT;
return false;
}
}
return false;
}
return false;
}
}
用法 - 添加到所需类的oncreate:
//create a swipe detector for items in the list
final SwipeDetector swipeDetector = new SwipeDetector();
//add a touch listener for the list view
mListView.setOnTouchListener(swipeDetector);
//also add a click listener and use it to get position in list
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Exercises exercises = new Exercises();
if (swipeDetector.swipeDetected()){
//get the object's position in the list
WorkoutExercises workoutExercises = mWorkoutExercisesAdapter.getItem(position);
//delete the object from DB
workoutExercises.deleteInBackground();
//notify user of the removal
Toast.makeText(getApplicationContext(), "Deleted from " + mWorkoutNameDisplay, Toast.LENGTH_LONG).show();
//update the view without the removed object
getCurrentExercisesInWorkout();
}
else {
//Item click
}
}
});
决定我向左或向右滑动以删除而不是向左滑动,但如果我只想做一个方向,我会使用滑动检测器方法的getAction:
if(swipeDetector.getAction()== SwipeDetector.Action.RL)
{
}