三步实现Android任意控件悬浮效果

Tag:

项目介绍:

之前做项目的时候实现的一个悬浮效果,如图(可能不够清晰)

meibei.gif
接下来就是实现效果,如图所示
sticky.gif
demo直接用的截图

原理很简单,用RecyclerView addHeaderView的方式实现,实现步骤:

1.添加依赖
`compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.14'

compile 'com.android.support:recyclerview-v7:25.1.0'`
导入BaseRecyclerViewAdapterHelper,用于RecyclerView添加HeaderView;

2.布局,用标签include,写入需要悬浮的view;主界面用帧布局FrameLayout

`<FrameLayout

   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <android.support.v7.widget.RecyclerView
       android:id="@+id/main_recycler"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
<include layout="@layout/include_header_product"/>

</FrameLayout> `;
然后布局headerView
` <ImageView

    android:layout_width="wrap_content"
    android:src="@mipmap/img_header_category"
    android:adjustViewBounds="true"
    android:layout_height="wrap_content"/>
<include layout="@layout/include_header_product"/>`

3.逻辑,滑动的时候,对RecyclerView进行滑动监听然后在onScrollStateChanged(RecyclerView recyclerView, int newState)方法里监听悬浮View在屏幕上Y轴位移,核心代码:

    ` int[] location = new int[2];

mImageView.getLocationOnScreen(location);

int y = location[1];
imageY = y;`
在onScrolled(RecyclerView recyclerView, int dx, int dy)里不停的获取headerView里面悬浮标签在屏幕上Y轴位移,进行判断悬浮view的显示或隐藏,核心代码:
` if (mHeaderView == null) return;

            int getTop = mHeaderView.getDistanceY();
            if (getTop <= imageY) {
                mImageView.setVisibility(View.VISIBLE);
            } else {
                mImageView.setY(0);
                mImageView.setVisibility(View.GONE);
            }`

github
demo体验地址

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值