您好,欢迎来到源码搜藏!分享精神,快乐你我!提示:担心找不到本站?在百度搜索“源码搜藏”,网址永远不丢失!
  • 首 页
  • 在线工具
  • 在RecyclerView上面实现的视差效果

    时间:2016-08-23 11:52 来源:互联网 作者:源码搜藏 浏览:收藏 挑错 推荐 打印

    第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); 

    结果

    ParallaxListView

    很酷的效果你可以用这个库

    • 透明的工具栏效果
    
    	
    @Override
    public void onParallaxScroll(float percentage, float offset, View parallax) {
      Drawable c = mToolbar.getBackground();
      c.setAlpha(Math.round(percentage * 255));
      mToolbar.setBackground(c);
    }

    ParallaxListView

    在RecyclerView上面实现的视差效果转载请注明出处http://www.codesocang.com/kj/recyclerviews/33502.html 源码搜藏网所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与源码搜藏网无关
    标签: