原理很简单,用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
3.逻辑,滑动的时候,对RecyclerView进行滑动监听然后在onScrollStateChanged(RecyclerView recyclerView, int newState)方法里监听悬浮View在 屏幕上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); }
资源均来自第三方,谨慎下载,前往第三方网站下载