RecyclerView添加一个头布局,默认的导航条和标题是透明的,当下滑过头布局的时候让导航条和标题显示。实现一个由透明变为不透明的过程。看图:

当下滑时导航条和标题由透明变成不透明
当下滑过头布局的时候  导航条显示
当下滑时导航条和标题由透明变成不透明

展示数据就不说了  直接说实现这个需求的代码:
      思路:设置ScrollView的监听事件

   首先让导航条和标题都是透明色
//Color.argb(0, 250, 250, 250)  RGB颜色转换 第一个参数表示透明度  后三位表示颜色  3个255表示白色  3个0表示黑色
    myLinearLayout.setBackgroundColor(Color.argb(0,250,250,250));
    myTextView.setTextColor(Color.argb(0,0,0,0));
//导航条下边的一个view
    myView.setBackgroundColor(Color.argb(0,199,199,199));
  
  然后设置RecyclerView的监听
myRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        //滑动的高度
        scrollY +=dy;
        //得到一个固定的高度 相当于上边的一个图片的高度
        mHeight = getResources().getDimensionPixelOffset(R.dimen.x310);
        //获得LinearLayout的高度
        mHeightTwo = myLinearLayout.getHeight();
        //获得状态烂的高度
        mStatusBarHeight = getStatusBarHeight();
        //获得状态栏和导航条的高度
        mFinalHeight = mHeightTwo+mStatusBarHeight;
        //滑动剩余的高度
        float v = mHeight - scrollY;

        if (v>0){
            if (v<mHeightTwo){
                int i = Float.valueOf((mHeightTwo-v)/mHeightTwo*255).intValue();
                myLinearLayout.getBackground().setAlpha(i);//i 有可能小于 0
                myLinearLayout.setBackgroundColor(Color.argb(i, 255, 255, 255));
                myView.setBackgroundColor(Color.argb(i, 199, 199, 199));
                myTextView.setTextColor(Color.argb(i, 51, 51, 51));
            }else {
                myLinearLayout.setBackgroundColor(Color.argb(0, 255, 255, 255));
                myView.setBackgroundColor(Color.argb(0, 199, 199, 199));
                myTextView.setTextColor(Color.argb(0, 51, 51, 51));
            }
        }else {
            myLinearLayout.setBackgroundColor(Color.argb(255, 255, 255, 255));
            myView.setBackgroundColor(Color.argb(255, 199, 199, 199));
            myTextView.setTextColor(Color.argb(255, 51, 51, 51));
        }
    }
});





相关文章:

  • 2022-12-23
  • 2021-11-20
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
相关资源
相似解决方案