第1步:添加JitPack库来构建文件
repositories {
maven {
url "https://jitpack.io"
}
}
第2步:添加的依赖
dependencies {
compile 'com.github.kanytu:android-parallax-recyclerview:v1.7'
}
用法
(示例项目- https://github.com/kanytu/example-parallaxrecycler)
- 创建对象列表,并把它传递给的构造函数
ParallaxRecyclerAdapter
List<String> myContent = new ArrayList<String>(); // or another object list ParallaxRecyclerAdapter<String> adapter = new ParallaxRecyclerAdapter<String>(content) { @Override public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<String> adapter, int i) { // If you're using your custom handler (as you should of course) // you need to cast viewHolder to it. ((MyCustomViewHolder) viewHolder).textView.setText(myContent.get(i)); // your bind holder routine. } @Override public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<String> adapter, int i) { // Here is where you inflate your row and pass it to the constructor of your ViewHolder return new MyCustomViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.myRow, viewGroup, false)); } @Override public int getItemCountImpl(ParallaxRecyclerAdapter<String> adapter) { // return the content of your array return myContent.size(); } };
- 现在,我们设置的视差头。你需要传递的
RecyclerView
也实行滚动听众。
myAdapter.setParallaxHeader(LayoutInflater.from(this).inflate( R.layout.myParallaxView, myRecycler, false), myRecyclerView);
在那里,你可以实现一些其他的听众:
//事件触发当你点击该适配器的项目。
oid onClick(View v, int position);//事件触发时的视差被滚动。
void onParallaxScroll(float percentage, float offset, View parallax);
结果
很酷的效果你可以用这个库
- 透明的工具栏效果
@Override public void onParallaxScroll(float percentage, float offset, View parallax) { Drawable c = mToolbar.getBackground(); c.setAlpha(Math.round(percentage * 255)); mToolbar.setBackground(c); }
资源均来自第三方,谨慎下载,前往第三方网站下载