简体   繁体   English

滑动手势素材设计

[英]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. 我当前使用ItemTouchHelper实施的问题是,项目始终总是从布局中完全删除,我不希望这种情况发生,如上面的视频所示,我希望允许布局以最大30%的比例向直接滑动。屏幕总宽度的百分比,如果用户将其放到屏幕上,我希望它进行备份,以便布局返回到其默认位置(不会部分移动到侧面)并且用户达到30屏幕上的百分比,以执行操作(例如保存该项目以供以后观看)并将该项目返回到其初始0%位置。 Whatsapp does something very similar to what I want to have, when swiped right a message, you can respod the message. Whatsapp的功能与我想要的功能非常相似,当向右滑动消息时,您可以重新发送消息。

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 我在stackOverflow中找到了它,这使我能够使布局不完全退出屏幕

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

But a few problems here. 但是这里有一些问题。

1 - Swipe speed is slow. 1-滑动速度慢。

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.) 2-当滑动到允许滑动的整个宽度的50%时,对象将继续其位置更改,将其保持在最大允许位置(我希望该项目在达到允许滑动的50%之后始终返回0%。)

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 我认为您需要的是这个库https://github.com/yanzhenjie/SwipeRecyclerView

In case you want to implement it yourself , have a look at the code of the library itself. 如果您想自己实现它 ,请查看 本身的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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