package com.lht.dot.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.bumptech.glide.Glide;

/**
 * Created by lht-Mac on 2017/7/11.
 */

public class PhotoShotRecyclerView extends RecyclerView {

    View mDispatchToView;

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

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

    private void init() {
        setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        Glide.with(getContext()).pauseRequests();
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                    case RecyclerView.SCROLL_STATE_IDLE:
                        Glide.with(getContext()).resumeRequests();
                        break;
                }
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if(mDispatchToView==null){
            return super.dispatchTouchEvent(event);
        }else{
            return mDispatchToView.dispatchTouchEvent(event);
        }
    }

    public View getDispatchToView() {
        return mDispatchToView;
    }

    public void setDispatchToView(View dispatchToView) {
        this.mDispatchToView = dispatchToView;
    }

    public void clearDispatchToView() {
        setDispatchToView(null);
    }
}
