仿探探滑动卡片

效果图

仿探探滑动卡片_第1张图片
GIF.gif

原谅我把妹子们换成了小机器人….

实现思路

利用自定义RecyclerView LayoutManager实现对item 的重新布局,只加载三个条目(可根据需求自定义),并将后面的两个按照比例缩放和位移,只不过三个的要倒序加载,否则最上面要展示的会addview,会被遮盖住.

实现代码

LayoutManager

package com.example.admin.kotlin.tantan;

import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by fushuang on 2017/7/20.
 */

public class TanTanLayoutManager extends RecyclerView.LayoutManager {

    private static final String TAG = "666666666666666666666";
    private static float scaleRate = 0.1f;
    private static float translateRate = 0.1f;
    private static int showCount = 3;
    public RecyclerView mRecyclerView;
    public OnSwipeListener listener;

    public TanTanLayoutManager(RecyclerView mRecyclerView, OnSwipeListener listener) {
        this.listener = listener;
        this.mRecyclerView = mRecyclerView;
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onLayoutChildren(final RecyclerView.Recycler recycler, RecyclerView.State state) {
        int itemCount = getItemCount();
        removeAllViews();
        if (itemCount > 0) {
            //倒序是因为要让第一个放在最上面,最后放
            for (int i = Math.min(showCount, itemCount) - 1; i >= 0; i--) {
                final View view = recycler.getViewForPosition(i);
                addView(view);
                measureChildWithMargins(view, 0, 0);
                int widthSpace = getWidth() - getDecoratedMeasuredWidth(view); //在宽度上的两边空白
                int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);  //高度上的两边空白
                layoutDecoratedWithMargins(view, widthSpace / 2, heightSpace / 2, widthSpace / 2 + getDecoratedMeasuredWidth(view), heightSpace / 2 + getDecoratedMeasuredHeight(view));
                //设置后面缩小和下移
                view.setScaleY(1 - (i * scaleRate));
                view.setScaleX(1 - (i * scaleRate));
                view.setTranslationY(view.getMeasuredHeight() * translateRate * i);
                if (i == 0) {
                    view.setOnTouchListener(new View.OnTouchListener() {
                        private float downX;
                        private ViewGroup.LayoutParams layoutParams;
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                                downX = event.getRawX();
                                layoutParams = v.getLayoutParams();
                                return true;
                            } else if (event.getAction() == MotionEvent.ACTION_UP) {

                                if (Math.abs(event.getRawX() - downX) > mRecyclerView.getMeasuredWidth() / 3) {
                                    listener.isSwipedGone(true);
                                } else {
                                    listener.isSwipedGone(false);
                                    v.setLayoutParams(layoutParams);
                                    return false;
                                }
                            }
                            v.setX(event.getRawX() - v.getMeasuredWidth() / 2);
                            v.setRotation((event.getRawX() - downX) / mRecyclerView.getMeasuredWidth() * 60);
                            v.setAlpha(1 - Math.abs((event.getRawX() - downX) / mRecyclerView.getMeasuredWidth()));
                            return false;
                        }
                    });
                }
            }
        }
    }
}

Activity

class MainActivity : AppCompatActivity(), OnSwipeListener {
    override fun isSwipedGone(gone: Boolean) {
        if (gone){
            mList.removeAt(0)
            mAdapter?.notifyDataSetChanged()
        }
    }

    private var mList = ArrayList()
    private var mAdapter: RecyclerAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        for (i in 0..20) {
            mList.add("str " + i)
        }
        mAdapter = RecyclerAdapter(mList, this)

        list.layoutManager= TanTanLayoutManager(list, this)
        list.adapter=mAdapter
    }
}

你可能感兴趣的:(仿探探滑动卡片)