简体   繁体   中英

Swipe gesture material design

I'm reading Cards - Material design . I'd like to implement swipe gesture in my layout, but I'm facing some problems with this.

This is the effect I want to get.

The problem with my current implementation with ItemTouchHelper is that the item is always completely removed from the layout, I do not want that to happen, as in the video above I would like to allow the layout to be swiped to the direct in the maximum 30% of the entire width of the screen, if the user drops it before it, I'd like it to back up so that the layout goes back to its default position (without being partially moved to the side) and if the user reaches 30% of the screen, to perform an action (such as saving the item to watch later) and returning the item to its initial 0% position. Whatsapp does something very similar to what I want to have, when swiped right a message, you can respod the message.

This is my code

    public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
    private RecyclerItemTouchHelperListener listener;
    RecyclerView recyclerView;

    public RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener, RecyclerView recyclerView) {
        super(dragDirs, swipeDirs);
        this.listener = listener;
        this.recyclerView = recyclerView;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        Log.i("waeaweaweaeawe", "onMove");
        return true;
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        Log.i("waeaweaweaeawe", "onSelectedChanged");
        if (viewHolder != null) {
            final View foregroundView = ((clv_exibirPosts.ViewHolder) viewHolder).container_post;

            getDefaultUIUtil().onSelected(foregroundView);
        }
    }

    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
                                RecyclerView.ViewHolder viewHolder, float dX, float dY,
                                int actionState, boolean isCurrentlyActive) {

    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        Log.i("waeaweaweaeawe", "clearView");
        final View foregroundView = ((clv_exibirPosts.ViewHolder) viewHolder).container_post;
        getDefaultUIUtil().clearView(foregroundView);
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder, float dX, float dY,
                            int actionState, boolean isCurrentlyActive) {





    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        Log.i("waeaweaweaeawe", "onSwiped");

    }

    @Override
    public int convertToAbsoluteDirection(int flags, int layoutDirection) {
        Log.i("waeaweaweaeawe", "convertToAbsoluteDirection");
        return super.convertToAbsoluteDirection(flags, layoutDirection);
    }

    public interface RecyclerItemTouchHelperListener {
        void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
    }
}

I have no idea how to set a maximum distance that swipe gesture can be dragged. I found this in stackOverflow, this makes me able to make the layout not completely exit the screen

super.onChildDraw(c, recyclerView, viewHolder, dX / 2, dY, actionState, isCurrentlyActive);

But a few problems here.

1 - Swipe speed is slow.

2 - When swiped to 50% of the entire width allowed to swiped, the object continues with its position changed, keeping it in its maximum allowable position (I want the item to always return to 0% after reaching 50% of swiped allowed.)

I've looked all over the internet and found no examples that talk about it. I'm sure this problem is not just mine. If you can give me some clues, examples of codes, posts, anything that might help I'll already be extremely grateful!

I think this library is what you need https://github.com/yanzhenjie/SwipeRecyclerView

In case you want to implement it yourself , have a look at the code of the library itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM