完美解决RecyclerView加载网络图片时自动

/*
复制粘贴就能用
无需设置RecyclerView的布局
*/
public class LinearRecyclerView extends RecyclerView implements RecyclerView.OnItemTouchListener,View.OnTouchListener{

    private boolean CanScroll = false;
    private addOnTouchEvent onTouchEvent;

    public LinearRecyclerView(Context context) {
        super(context);
        init(context);
    }

    public LinearRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LinearRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context){
        setLayoutManager(new LinearLayoutManager(context){
            @Override
            public boolean canScrollVertically() {
                return CanScroll;
            }

        });
        addOnItemTouchListener(this);
        setOnTouchListener(this);
    }


    public void setOnTouchEvent(addOnTouchEvent onTouchEvent) {
        this.onTouchEvent = onTouchEvent;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        CanScroll = true;
        if(this.onTouchEvent != null)
            return this.onTouchEvent.onTouchEvent(v,event);
        return false;
    }

    public interface addOnTouchEvent{
        boolean onTouchEvent(View v, MotionEvent event);
    }


    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        CanScroll = true;
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        CanScroll = true;
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        CanScroll = true;
    }

    public void setCanScroll(boolean c){
        CanScroll = c;
    }
}
 
  
Fragment切换时也会出现RecyclerView自动滑动,可以在隐藏Fragment的时候调用setCanScroll(false);

你可能感兴趣的:(Android,RecyclerView)