RecyclerView多布局 下拉头部放大

public class GroupTopicRecyclerView extends RecyclerView {
    private static final String TAG = "MyRecyclerView";
    private LinearLayoutManager mLayoutManager;
    private int mTouchSlop;
    private View zoomView;
    // 记录首次按下位置
    private float mFirstPosition = 0;
    // 是否正在放大
    private Boolean mScaling = false;

    LinearLayoutManager mLinearLayoutManager;
    private int screenWidth;

    public GroupTopicRecyclerView(Context context) {
        this(context, null);
    }

    public GroupTopicRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GroupTopicRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mLayoutManager = new LinearLayoutManager(context);
        setLayoutManager(mLayoutManager);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public void setZoomView(View v, LinearLayoutManager linearLayoutManager) {
        //TODO获取屏幕宽度
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
       // System.out.println("宽度是 =  " + screenWidth);

        //此处我的图片命名为img,大家根据实际情况修改
        SimpleDraweeView img = v.findViewById(R.id.details_one_titleimg);
        RelativeLayout re = v.findViewById(R.id.details_one_background);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) img.getLayoutParams();
        //获取屏幕宽度
        lp.width = screenWidth;

        //设置宽高比为16:9
        lp.height = screenWidth * 14 / 25;
        //给imageView重新设置宽高属性
        img.setLayoutParams(lp);
        re.setLayoutParams(lp);
        this.zoomView = img;
        mLinearLayoutManager = linearLayoutManager;

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (zoomView != null) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) zoomView.getLayoutParams();
            //判断触摸事件
            switch (event.getAction()) {
                //触摸结束
                case MotionEvent.ACTION_UP:
                    mScaling = false;
                    replyImage();
                    break;
                //触摸中
                case MotionEvent.ACTION_MOVE:
                    //判断是否正在放大 mScaling 的默认值为false
                    if (!mScaling) {
                        //当图片也就是第一个item完全可见的时候,记录触摸屏幕的位置
                        if (mLinearLayoutManager.findViewByPosition(mLinearLayoutManager.findFirstVisibleItemPosition()).getTop() == 0) {
                            //记录首次按下位置
                            mFirstPosition = event.getY();
                        } else {
                            break;
                        }
                    }
                    // 滚动距离乘以一个系数
                    int distance = (int) ((event.getY() - mFirstPosition) * 0.4);
           
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值