列表中使用Picasso加载图片滑动列表时加载效率优化解决滑动卡顿问题

1、ListView优化:
重新自定义ScrollListener并设置滑动事件
mListView.setOnScrollListener(new ScrollListenerListView(this));

自定义的ScrollListener如下:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.widget.AbsListView;

import com.manyiaby.view.viewutils.LogUtil;
import com.squareup.picasso.Picasso;
/**
* 列表滚动时Picasso暂停加载停止滚动时恢复加载事件
* Created by ${cd} on 2018/1/29.
*/

public class ScrollListenerListView implements AbsListView.OnScrollListener{
private String TAG = getClass().getSimpleName();
private final Context context;

/**
 * 列表滚动时Picasso暂停加载停止滚动时恢复加载事件
 * @param context
 */
public ScrollListenerListView(Context context) {
    this.context = context;
}

/**
 * @param view e.g:ListView
 * @param scrollState
 *  0(SCROLL_STATE_IDLE)表示ListView是不动的(The AbsListView is not currently scrolling.)
 *  1(SCROLL_STATE_DRAGGING)表示ListView正在被拖拽(The AbsListView is currently being dragged by outside input such as user touch input.)
 *  2(SCROLL_STATE_SETTLING)表示ListView正在惯性下滚动(The AbsListView is currently animating to a final position while not under outside control.)
 */
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    LogUtil.i(TAG,"=====" + 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.
}

}

2、RecyclerView优化:
重新自定义ScrollListener并设置滑动事件
mRecyclerView.addOnScrollListener(new ScrollListenerRecyclerView(this));

自定义的ScrollListener如下:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import com.manyiaby.view.viewutils.LogUtil;
import com.squareup.picasso.Picasso;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_SETTLING;

/**
* 列表滚动时Picasso暂停加载停止滚动时恢复加载事件
* Created by ${cd} on 2018/1/29.
*/

public class ScrollListenerRecyclerView extends RecyclerView.OnScrollListener{
private String TAG = getClass().getSimpleName();
private final Context context;

/**
 * 列表滚动时Picasso暂停加载停止滚动时恢复加载事件
 * @param context
 */
public ScrollListenerRecyclerView(Context context) {
    this.context = context;
}

/**
 * @param recyclerView
 * @param scrollState
 *  0(SCROLL_STATE_IDLE)表示recyclerView是不动的(The recyclerView is not currently scrolling.)
 *  1(SCROLL_STATE_DRAGGING)表示recyclerView正在被拖拽(The recyclerView is currently being dragged by outside input such as user touch input.)
 *  2(SCROLL_STATE_SETTLING)表示recyclerView正在惯性下滚动(The recyclerView is currently animating to a final position while not under outside control.)
 */
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
    super.onScrollStateChanged(recyclerView,scrollState);
    LogUtil.i(TAG,"=====" + scrollState);
    final Picasso picasso = Picasso.with(context);
    if(scrollState == SCROLL_STATE_SETTLING){
        picasso.pauseTag(context);//列表滑动的时候调用,停止加载图片
    }else{
        picasso.resumeTag(context);//在停止滚动/正在滚动的时候调用,停止滚动时恢复图片加载
    }
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
}

}

补充:
1、注意ListView中和RecyclerView中的设置方法不同
ListView:setOnScrollListener
RecyclerView:addOnScrollListener
2、ListView中和RecyclerView的自定义ScrollListener不同
ListView:ScrollListenerListView implements AbsListView.OnScrollListener
RecyclerView:ScrollListenerRecyclerView extends RecyclerView.OnScrollListener
ListView重写ScrollListener需要实现OnScrollListener接口(这个可以由源码验证)
RecyclerView重写ScrollListener需要继承抽象类OnScrollListener(这个可以由源码验证)
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/pillar1066527881/article/details/79197597