Coder Social home page Coder Social logo

suitedrecyclerview's Introduction

效果图

最基本的用法

  • RecyclerView 的设置

      mPhotosAdapter = new PhotosAdapter(list, this);
      recyclerView.setAdapter(mPhotosAdapter);
      //自定义LayoutManager
      SuitedLayoutManager layoutManager = new SuitedLayoutManager(mPhotosAdapter);
      recyclerView.setLayoutManager(layoutManager);
      //设置最大的图片显示高度,默认为600px
      layoutManager.setMaxRowHeight(getResources().getDisplayMetrics().heightPixels / 3);
      //设置Item之间的空隙
      recyclerView.addItemDecoration(new SuitedItemDecoration(DisplayUtils.dpToPx(4.0f, this)));
    
  • PhotosAdapter需要实现SizeCaculator.SizeCalculatorDelegate接口里的aspectRatioForIndex(int position)方法,返回图片宽高比

      @Override
      public double aspectRatioForIndex(int position) {
        if (position < getItemCount()) {
            PhotoInfo info = mPhotos.get(position);
            //如果你的图片url是以_w750_h750.jpg这样的格式结尾,可以用SuitUrlUtil这个工具类获取它的宽高比
            double ratio = SuitUrlUtil.getAspectRadioFromUrl(info.photo);
            return ratio;
        }
        return 1.0;
      } 
    

##内存的优化使用

由于这个LayoutManager会根据自身宽高和相邻图片宽高的比率最后计算出每张图片的大小,所以每张图片大小几乎都不一样,recyclerView里View的复用情况很少,这种情况下如果不对图片进行等比缩小和对滑动做优化,将会造成严重的卡顿。

在我个人实践的过程中呢,有两处优化的点可以让图片滑动时更为流畅。

  • 在ImageView确定Width和Height后,再进行网络请求,具体可参考library里的SuitImageView的做法

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          if(w != 0 && h != 0) {
              Picasso.with(getContext()).load(mPhoto).tag(getContext()).resize(w,h).into(this);
        }
    
  • 滑动过程中,中断图片请求,以使用Picasso框架请求图片为例:

      recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
              super.onScrollStateChanged(recyclerView, newState);
              if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                  Picasso.with(PhotoActivity.this).resumeTag(PhotoActivity.this);
              } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                  Picasso.with(PhotoActivity.this).pauseTag(PhotoActivity.this);
              } else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
                  Picasso.with(PhotoActivity.this).pauseTag(PhotoActivity.this);
              }
          }
      });
    

##Demo的滑动效果

下拉刷新上拉加载更多框架修改自WaveSwipeRefreshLayout开源库

##License Apache 2.0

suitedrecyclerview's People

Contributors

asdzheng avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.