【问题标题】:Swipe getItem(position) in listview在列表视图中滑动 getItem(position)
【发布时间】:2015-05-27 20:17:27
【问题描述】:

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

谢谢大家!

【问题讨论】:

    标签: android listview swipe


    【解决方案1】:

    我做了一些研究,并查看了 StackOverflow 上的其他一些示例。

    我已经实现了一个 Swipe 检测器类:

    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)
    {
    
    }
    

    【讨论】:

    • 感谢您的解决方案,它可以工作...但是在第一次滑动检测之后,除非以某种方式刷新 ListView,否则它不起作用。有什么想法吗?
    • 是的,您需要让 listview 适配器知道数据集在删除后已更改:mListViewAdapter.notifyDataSetChanged();仅供参考,我从列表视图继续前进,它们对我来说太过时了,我需要更流畅的东西来频繁添加和删除项目,最终使用了 Recyclerview。
    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多