gpt4 book ai didi

android - 在 ListView 中滑动 getItem(position)

转载 作者:行者123 更新时间:2023-11-30 02:29:08 25 4
gpt4 key购买 nike

好的,我有一个 ListView,工作正常,从数据库获取数据。想要在 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 - position 作为参数。

public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

点击时它起作用并通过位置 - 当然唯一的问题是我必须先点击然后向左滑动。如何传递/存储我滑动的位置以传递给 onSwipeLeft

非常感谢!

最佳答案

我做了一些研究并查看了 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)
{

}

关于android - 在 ListView 中滑动 getItem(position),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27515877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com