RecyclerView顶部Item增加消失动画效果

参考文章
自定义的RecyclerView

package com.example.test0908;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author writing
 * @time 2020/9/1110:24
 * @note
 */
public class MyRecyclerView extends RecyclerView {

    private LinearLayoutManager linearLayoutManager;
    private View firstView;
    private View lastView;

    public MyRecyclerView(@NonNull Context context) {
        super(context);
        init();
    }

    public MyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
    }

    @Override
    public void setLayoutManager(@Nullable LayoutManager layout) {
        super.setLayoutManager(layout);
        linearLayoutManager = (LinearLayoutManager) layout;
    }

    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);

        Log.i("zhang_xin1", "onScrolled:dx:" + dx + ",dy:" + dy);
        Log.i("zhang_xin2", "canScrollVertically" + canScrollVertically(-1));

        //表示能否向下滚动
        if (canScrollVertically(-1)) {
            if(lastView!=null){
                lastView.setAlpha(1f);
            }
            int first = linearLayoutManager.findFirstVisibleItemPosition();
            firstView = linearLayoutManager.findViewByPosition(first);


            if (firstView.getTop() < 0) {

                Log.i("zhang_xin2", "Math.abs(getTop())/firstView.getHeight():" + (float)Math.abs(firstView.getTop())+"        "+firstView.getHeight());
                firstView.setAlpha(1-(float) Math.abs(firstView.getTop())/firstView.getHeight());
            }
            lastView = firstView;
        }else{
             firstView = linearLayoutManager.findViewByPosition(0);
             firstView.setAlpha(1f);
        }
        //表示能否向上滚动
//        if(canScrollVertically(1)){
//            int first = linearLayoutManager.findFirstVisibleItemPosition();
//            View firstView = linearLayoutManager.findViewByPosition(first+1);
//            Log.i("zhang_xin2", "firstView.getTop():" + firstView.getTop());
//            if (firstView.getTop()>0) {
//                firstView.setAlpha(1f);
//            }
//        }
    }

    @Override
    public void onScreenStateChanged(int screenState) {
        super.onScreenStateChanged(screenState);
    }

    @Override
    public boolean canScrollHorizontally(int direction) {
        return super.canScrollHorizontally(direction);
    }

    @Override
    public boolean canScrollVertically(int direction) {
        return super.canScrollVertically(direction);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值