0

Is there any demo or example to make the swipe behavior present on Material.IO Documentation? https://material.io/components/cards#behavior

I cant find a way to have a real sense of move and make the color animation.. Basically I don't know how to make this example.

Thanks!

Nuno Oliveira
  • 33
  • 1
  • 6

2 Answers2

0

If you are using a RecyclerView, then you can achieve swipe functionality using this documentation from codelabs. Hope this helps.

https://codelabs.developers.google.com/codelabs/android-training-cards-and-colors/index.html?index=..%2F..android-training#4

Update:

This post might help you if you are aiming for something you've posted.

Bharath
  • 391
  • 2
  • 9
  • I want anything like that https://kstatic.googleusercontent.com/files/7e4ab89fb77bd37859c71e8625ec5d3a0d1ecaedcb888e9e4af37f0132c2938917ca5d6598145e4cc2419f815bd1248a82fbac123c49538bc0d0d8046fcc76cd only swipe a little – Nuno Oliveira Apr 22 '20 at 10:52
  • nope, not works, i can make it work with drag on itemtouchhelper but i cant know when he drops the card – Nuno Oliveira Apr 23 '20 at 01:04
0

I got this working with:

ItemTouchHelper.Callback itemTouchCallback = new ItemTouchHelper.Callback() {
            private boolean swipeBack = false;
            private boolean hitMax = false;
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                if (viewHolder instanceof LocationsDetailAdapter.LocationsDetailViewHolder) {
                    int swipeFlags = ItemTouchHelper.END;
                    return makeMovementFlags(0, swipeFlags);
                } else
                    return 0;
            }

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return true;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                hitMax = false;
                getDefaultUIUtil().clearView(((LocationsDetailAdapter.LocationsDetailViewHolder) viewHolder).getForeground());
            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (viewHolder != null) {
                    getDefaultUIUtil().onSelected(((LocationsDetailAdapter.LocationsDetailViewHolder) viewHolder).getForeground());
                }
            }

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                recyclerView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                            swipeBack = true;
                        } else {
                            swipeBack = false;
                        }
                        return false;
                    }
                });
                if(dX >= 150f) {
                    dX = 150f;
                    if (!hitMax) {
                        setCardFavorite(arrayLockers.get(viewHolder.getAdapterPosition()), (LocationsDetailAdapter.LocationsDetailViewHolder) viewHolder);
                    }
                    hitMax = true;
                }
                getDefaultUIUtil().onDraw(c, recyclerView, ((LocationsDetailAdapter.LocationsDetailViewHolder) viewHolder).getForeground(), dX, dY, actionState, isCurrentlyActive);
            }

            @Override
            public int convertToAbsoluteDirection(int flags, int layoutDirection) {
                return swipeBack ? 0 : super.convertToAbsoluteDirection(flags, layoutDirection);
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);


        return view;
    }
public void setCardFavorite(Locker locker, LocationsDetailAdapter.LocationsDetailViewHolder holder){
        final RelativeLayout background_selected = holder.getBackground_selected();
        final CardView card = holder.getLocationDetailCard();

        Animator anim;
        if(!locker.isFavorite()){
            anim = ViewAnimationUtils.createCircularReveal(background_selected, 75, 75, 0, (float) Math.hypot(background_selected.getWidth(), background_selected.getHeight()));
            background_selected.setVisibility(View.VISIBLE);
            final float scale = getResources().getDisplayMetrics().density;
            int pixels = (int) (getResources().getDimension(R.dimen.spacing_small) * scale + 0.5f);
            ViewGroup.MarginLayoutParams layoutParams =
                    (ViewGroup.MarginLayoutParams) card.getLayoutParams();
            layoutParams.setMargins(pixels, 0, 0, 0);
            card.requestLayout();
            locker.setFavorite(true);
            Toast.makeText(getContext(), "Adicionou a fechadura aos favoritos!", Toast.LENGTH_SHORT).show();
            anim.setDuration(1000);
        } else {
            anim = ViewAnimationUtils.createCircularReveal(background_selected, 75, 75, (float) Math.hypot(background_selected.getWidth(), background_selected.getHeight()), 0);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    background_selected.setVisibility(View.INVISIBLE);
                }
            });
            ViewGroup.MarginLayoutParams layoutParams =
                    (ViewGroup.MarginLayoutParams) card.getLayoutParams();
            layoutParams.setMargins(0, 0, 0, 0);
            card.requestLayout();
            locker.setFavorite(false);
            Toast.makeText(getContext(), "Removeu a fechadura dos favoritos!", Toast.LENGTH_SHORT).show();
            anim.setDuration(300);

        }
        anim.start();
    }
Nuno Oliveira
  • 33
  • 1
  • 6