item中有图片加载时,让RecyclerView滑动更流畅

RecyclerView 为了滑动更加流畅,滑动时停止加载图片,反之滑动停止时加载图片。
则对RecyclerView 进行重写:


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.bumptech.glide.Glide;

public class XRecyclerView extends RecyclerView {
    public XRecyclerView(Context context) {
        this(context, null); 
     } 
 
    public XRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0); 
     } 
 
    public XRecyclerView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
         init(); 
     } 
 
    private void init() { 
        addOnScrollListener(new ImageAutoLoadScrollListener()); 
     } 
 
    //监听滚动来对图片加载进行判断处理 
     public class ImageAutoLoadScrollListener extends OnScrollListener{ 
 
        @Override 
         public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
            super.onScrolled(recyclerView, dx, dy); 
         } 
 
        @Override 
         public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
            super.onScrollStateChanged(recyclerView, newState); 
             switch (newState){ 
                case SCROLL_STATE_IDLE: // The RecyclerView is not currently scrolling. 
                     //当屏幕停止滚动,加载图片 
                     try { 
                        if(getContext() != null) Glide.with(getContext()).resumeRequests();
                    } 
                    catch (Exception e) { 
                        e.printStackTrace(); 
                     } 
                    break; 
                 case SCROLL_STATE_DRAGGING: // The RecyclerView is currently being dragged by outside input such as user touch input. 
                     //当屏幕滚动且用户使用的触碰或手指还在屏幕上,停止加载图片 
                     try { 
                        if(getContext() != null) Glide.with(getContext()).pauseRequests(); 
                     } 
                    catch (Exception e) { 
                        e.printStackTrace(); 
                     } 
                    break; 
                 case SCROLL_STATE_SETTLING: // The RecyclerView is currently animating to a final position while not under outside control. 
                     //由于用户的操作,屏幕产生惯性滑动,停止加载图片 
                     try { 
                        if(getContext() != null) Glide.with(getContext()).pauseRequests(); 
                     } 
                    catch (Exception e) { 
                        e.printStackTrace(); 
                     } 
                    break; 
             } 
        } 
    } 
} 


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值