Implementing “withDelay” in Picasso Android (for skimming)

后端 未结 2 1725
南方客
南方客 2020-12-06 15:10

When dealing with many scrolling images, you have to avoid the problem of loading while skimming, while the user is fast scrolling. The simplest and often best solution is r

相关标签:
2条回答
  • 2020-12-06 15:21

    The ability to pause/resume a request will be part of the next release of Picasso 2.4 Just yesterday a pull request containing this feature has been merged in the main branch. It allows to easily define a scroll listener that pauses/resume Picasso while flinging, as shown in the sample code reported below:

    public class SampleScrollListener implements AbsListView.OnScrollListener {
      private final Context context;
    
      public SampleScrollListener(Context context) {
        this.context = context;
      }
    
      @Override
      public void onScrollStateChanged(AbsListView view, int scrollState) {
        final Picasso picasso = Picasso.with(context);
        if (scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_TOUCH_SCROLL) {
          picasso.resumeTag(context);
        } else {
          picasso.pauseTag(context);
        }
      }
    
      @Override
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                           int totalItemCount) {
        // Do nothing.
      }
    }
    
    0 讨论(0)
  • 2020-12-06 15:22

    edit:

    so apparently the guys at Square just starting to move forward with their stuff. https://github.com/square/picasso/pull/665

    so Lucasr took over and re-organize some of the code. Now the pause/resume can be done in groups, all requests have a DEFAULT_GROUP and apparently the ScrollListener idea was ditched because it's too much of a simple implementation for them to bother, but it's the same code @a.bertucci posted.

    public class SampleScrollListener implements AbsListView.OnScrollListener {
      private final Context context;
      private static final Object scrollTag = new Object(); // this can be static or not, depending what u want to achieve
    
      public SampleScrollListener(Context context) {
        this.context = context;
      }
    
      @Override
      public void onScrollStateChanged(AbsListView view, int scrollState) {
        final Picasso picasso = Picasso.with(context);
        if (scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_TOUCH_SCROLL) {
          picasso.resumeTag(scrollTag);
        } else {
          picasso.pauseTag(scrollTag);
        }
      }
    
      @Override
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                           int totalItemCount) {
        // Do nothing.
      }
    }
    

    this implementation goes with the idea that you'll tag your requests with the context apparently, but just as easily you could tag with default or your custom tags.

    original answer:

    There's already a PullRequest on Picasso for this feature here: https://github.com/square/picasso/pull/561

    It's a bit different from what you suggested, but it works great (I'm using on my app). You have an option to pause/resume dispatching the images to the ImageViews and use an onScrollListener to pause n resume it.

    The code for it is simply:

    listView.setOnScrollListener(new PicassoScrollListener(context));
    

    I agree that forks are annoying because they can get outdated, but can fork it yourself, and keep it up-to-date until it gets merged on Picasso.

    • fork Picasso original
    • add this as a remote https://github.com/sockeqwe/picasso and fetch it
    • create your branch out of picasso/master and cherry pick those 10 commits from sockeqwe/picasso
    • pull picasso/master as often as you want

    It's not ideal, but the programming is all done for you and it works very well.

    Alternatively you can use my app fork compile 'com.eyeem.picasso:picasso:2.3.3-SNAPSHOT' and keep an eye on that pull request until it gets merged and you revert.

    0 讨论(0)
提交回复
热议问题