三步实现控件悬浮


代码下载:  http://download.csdn.net/detail/shimiso/9831046

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

1.添加依赖

1. compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.14'
2. compile 'com.android.support:recyclerview-v7:25.1.0'

导入BaseRecyclerViewAdapterHelper,用于RecyclerView添加HeaderView;

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

01.
02. android:layout_width="match_parent"
03. android:layout_height="match_parent">
04.
05. android:id="@+id/main_recycler"
06. android:layout_width="match_parent"
07. android:layout_height="match_parent"/>
08. "@layout/include_header_product"/>
09.  
10.
11. 然后布局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); 

}

你可能感兴趣的:(Android开发)