在 Android 的列表视图中滑动时显示删除按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12713926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:30:38  来源:igfitidea点击:

Showing a delete button on swipe in a listview for Android

androidlistviewonfling

提问by Andy

Expanding on another Stackoverflow question, I've implemented some gesture detection code so that I can detect when a row in my listview (which is in a FrameLayout) has been swiped. I followed the question/answer by Damian here about how to get the individual row/view from the adapter. How to get location (on screen) of row in listview

扩展另一个 Stackoverflow 问题,我已经实现了一些手势检测代码,以便我可以检测到我的列表视图中的一行(在 FrameLayout 中)何时被刷过。我按照 Damian 在这里关于如何从适配器获取单个行/视图的问题/答案进行了操作。 如何在列表视图中获取行的位置(在屏幕上)

I have code in my onFling that gets the view for the row, and tries to make a delete button that is set as invisible in my xml layout to visible. However, this doesn't happen. I was wondering how I make a button visible in a listview on a swipe?

我的 onFling 中有代码可以获取行的视图,并尝试使在我的 xml 布局中设置为不可见的删除按钮可见。然而,这不会发生。我想知道如何在滑动时在列表视图中显示按钮?

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {

            if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                int itemId = MyClass.this.lv.pointToPosition(
                        (int) e1.getX(), (int) e1.getY());

                Log.v("item id", String.valueOf(itemId));
                View v = MyClass.this.adapter
                        .getViewOnScreen(itemId);
                Button delete = (Button) v.findViewById(R.id.button_delete);

                delete.setVisibility(View.VISIBLE);
                //MyClass.this.adapter.notifyDataSetChanged();


            }

        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}

My list adapter code is the same as the referenced question.

我的列表适配器代码与引用的问题相同。

Edit: I tried using getChildAt() on the listview to get the row's view, and this works when there is one screen or less of items, but when there's more than the wrong view is returned and therefore the wrong delete button becomes visible.

编辑:我尝试在 listview 上使用 getChildAt() 来获取行的视图,这在只有一个屏幕或更少的项目时有效,但是当返回的视图多于错误时,因此错误的删除按钮变得可见。

Edit 2: I used the answer on the question hereto get it to work:

编辑 2:我在这里使用了这个问题的答案来让它工作:

回答by Heskja

I implemented something like this in my app once. The way I did it:

我曾经在我的应用程序中实现了类似的东西。我是这样做的:

public class MyGestureDetector extends SimpleOnGestureListener {
    private ListView list;

    public MyGestureDetector(ListView list) {
        this.list = list;
    }

    //CONDITIONS ARE TYPICALLY VELOCITY OR DISTANCE    
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (INSERT_CONDITIONS_HERE)
            if (showDeleteButton(e1))
                return true;
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    private boolean showDeleteButton(MotionEvent e1) {
        int pos = list.pointToPosition((int)e1.getX(), (int)e1.getY());
        return showDeleteButton(pos);
    }

    private boolean showDeleteButton(int pos) {
        View child = list.getChildAt(pos);
        if (child != null){
            Button delete = (Button) child.findViewById(R.id.delete_button_id);
            if (delete != null)
                if (delete.getVisibility() == View.INVISIBLE)
                    delete.setVisibility(View.VISIBLE);
                else
                    delete.setVisibility(View.INVISIBLE);
            return true;
        }
        return false;
    }
}

This worked for me, hope you'll get it to work or that it at least gives you some hint.

这对我有用,希望你能让它工作,或者它至少给你一些提示。