Android ListView Swipe for Action like Twitter List

蹲街弑〆低调 提交于 2019-12-02 18:37:46

Use a GestureDetector in each row, and wrap the row contents itself in a ViewFlipper. On a swipe, switch children of the ViewFlipper.

I have a ViewSwiper that combines a GestureDetector and ViewFlipper, but it is designed to work in either direction (e.g., from the regular row, a swipe left or right would switch to the actions), which may or may not be desirable. But, it should give you an idea of how this might work.

MariuszP

I did something similiar to what you need - you can find some info about there. I'm doing some manual stuff there but it works nice. For your convenience here is the code I've used:

OnTouchListener gestureListener = new View.OnTouchListener() {

    private int padding = 0;
    private int initialx = 0;
    private int currentx = 0;
    private ViewHolder viewHolder;

    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            padding = 0;
            initialx = (int) event.getX();
            currentx = (int) event.getX();
            viewHolder = ((ViewHolder) v.getTag());
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            currentx = (int) event.getX();
            padding = currentx - initialx;
        }

        if (event.getAction() == MotionEvent.ACTION_UP ||
            event.getAction() == MotionEvent.ACTION_CANCEL) {
            padding = 0;
            initialx = 0;
            currentx = 0;
        }

        if (viewHolder != null) {
            if (padding == 0) {
                v.setBackgroundColor(0xFF000000);
                if (viewHolder.running)
                    v.setBackgroundColor(0xFF058805);
            }
            if (padding > 75) {
                viewHolder.running = true;
                v.setBackgroundColor(0xFF00FF00);
                viewHolder.icon.setImageResource(R.drawable.clock_running);
            }
            if (padding < -75) {
                viewHolder.running = false;
                v.setBackgroundColor(0xFFFF0000);
            }
            v.setPadding(padding, 0, 0, 0);
        }
        return false;
    }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!