一个用于放置在RecycleView中的图片控件,其主要功能是跟随列表的上下滚动而上下平移,使得呈现出一种图像相对列表静止的感受。html
Overview
ScrollingImageView 提供如下特性:android
在垂直方向上,使得该组件随着列表滚动而上线爱平移,可选参数:scrollType(middle | whole)。
scrollType为whole时, 从刚进入列表和彻底出列表是都在滚动布局
scrollType: middle时,仅当item完整展示在列列表中时才滚动post
Demo 示例代码spa
Gradle integrationcode
该库托管于jCenter仓库中,使用时只须要添加如下代码xml
dependencies { compile 'site.okhttp.codeyel:scrollingImageView:1.0.0' } Basic uasge
第一步:在列表的单个item中的对应布局中引入htm
ScrollingImageView,例如:(item.xml)
<com.yel.image.ScrollingImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="200dp" custom:scrollType="middle" />
第二步:在代码中添加监听器blog
常规代码:索引
final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(layoutManager);
须要添加的代码:
recyclerView.addOnScrollListener(new ScrollingImageView.ScrollListener(layoutManager, R.id.img, index));
其中 {index} 表示item在列表中的索引值, {R.id.img} 对应android:id="@+id/img"
Advance uasge
开放自定义滚动的接口
Principle
重写setFrame方法:经过postTranslate方法垂直平移图片,从而显示图片中的不一样位置。
设置OnScrollListener监听器,监听列表的滚动位置,来计算得到恰当的平移参数,并将其传递给步骤①中的postTranslate方法。
以下图所示
原图:
当在该控件顶部和在中部时:
平移时的先后对比
源码下载地址:Android图片控件,跟随列表(recyclerView)的上下滚动而同步平移。
源码下载地址