Android ListView Swipe for Action like Twitter List

ぃ、小莉子 提交于 2019-12-20 09:25:05

问题


I'm trying to implement the "swipe for action" feature - like you can see in the "new" twitter app. A closer description you find at swipe for action description.

Now, are there any ideas/solution how to implement this feature?

Thanks!


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/6556909/android-listview-swipe-for-action-like-twitter-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!