显示上轻扫删除按钮在ListView为Android按钮、上轻扫、Android、ListView

2023-09-05 09:39:35 作者:梦醒人离

扩大的另一个问题#1,我已经实现了一些手势的检测code,这样我可以检测到一排在我的列表视图(这是一个的FrameLayout)已被偷走。我接着达米安这里如何让各行/视图从适配器的问题/答案。 How让排在列表视图中的位置(在屏幕上)

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

我有code在我onFling,获取视图的行,并试图做出一个删除按钮被设置为不可见在我的XML布局可见。但是,这并没有发生。我想知道我是怎么做的就刷卡列表视图按钮可见?

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?

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;
    }
}

我的目录适配器code是一样的引用问题。

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

编辑:我尝试使用getChildAt()的列表视图,以获取该行认为,这工作时,有一个屏幕或更小的项目,但如果有一个以上的错误观点返回,因此错误的删除按钮变为可见的。

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.

编辑2:我以前对这个问题答案这里得到它的工作:

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

推荐答案

我在我的应用程序实现这样的事情一次。就是我做的:

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.

 
精彩推荐