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

来源:互联网 发布:python中的readline 编辑:程序博客网 时间:2024/05/27 08:13
/*
复制粘贴就能用
无需设置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);
阅读全文
0 0
原创粉丝点击