<commit_before>package cn.diaovision.omnicontrol.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import java.util.ArrayList;
import java.util.List;

import cn.diaovision.omnicontrol.R;
import cn.diaovision.omnicontrol.core.model.conference.Term;

/**
 * Created by TaoYimin on 2017/5/4.
 */
public class SlidingItemView extends RelativeLayout implements View.OnClickListener {
    private Scroller mScroller;

    private float downX, dispatchDownX, dispatchDownY;

    private boolean isOpen;

    private VelocityTracker mVelocityTracker;

    private View convertView;

    private View hideView;

    private int position;

    private List<Term> list;

    public OnHideViewClickListener onHideViewClickListener;

    //手指是否up
    private boolean isActionUp = false;

    private int hideViewWidth;

    public SlidingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    public void setHideView() {
        if (hideView == null) {
            createHideView();
            if (convertView instanceof RelativeLayout) {
                ((RelativeLayout) convertView).addView(hideView);
                LayoutParams params = (LayoutParams) hideView
                        .getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                hideView.setLayoutParams(params);
                bringToFront();
            }
        }
    }

    private void createHideView() {
        hideView = LayoutInflater.from(getContext()).inflate(R.layout.layout_hide, this, false);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                dispatchDownX = event.getX();
                dispatchDownY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                if (Math.abs(dispatchDownX - event.getX()) < 10
                        && Math.abs(dispatchDownY - event.getY()) < 10) {
                    View parent = (View) convertView.getParent();
                    if (canRemove()) {
                        int width = hideViewWidth;
                        if (dispatchDownX < getWidth() - width) {
                            ViewGroup viewGroup = (ViewGroup) parent;
                            int count = viewGroup.getChildCount();
                            for (int i = 0; i < count; i++) {
                                View convertView = viewGroup.getChildAt(i);
                                SlidingItemView slidingItemView = (SlidingItemView) convertView
                                        .getTag(convertView.getId());
                                if (slidingItemView.canRemove()
                                        && slidingItemView != this) {
                                    slidingItemView.scrollToEnd(null, 0, 200);
                                }
                            }
                        }
                        return super.dispatchTouchEvent(event);
                    }

                    ViewGroup viewGroup = (ViewGroup) parent;
                    int count = viewGroup.getChildCount();
                    boolean isExistOpenItem = false;
                    for (int i = 0; i < count; i++) {
                        View convertView = viewGroup.getChildAt(i);
                        SlidingItemView slidingItemView = (SlidingItemView) convertView
                                .getTag(convertView.getId());
                        if (slidingItemView.canRemove()) {
                            isExistOpenItem = true;
                            slidingItemView.scrollToEnd(null, 0, 200);
                        }
                    }
                    if (!isExistOpenItem) {
                        convertView.performClick();
                    }
                    return false;
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        createVelocityTracker(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX() + getScrollX();
                if (canRemove()
                        && event.getX() > getWidth() - hideView.getWidth()) {
                    return false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int distance = (int) (event.getX() - downX);
                if (getScrollX() > hideView.getWidth() / 10) {
                    isOpen = true;
                }
                // 当左移,滑动操作被禁用
                if (distance > 0) {
                    scrollTo(0, 0);
                    return false;
                } else if (distance < -hideView.getWidth()) {
                    scrollTo(hideView.getWidth(), 0);
                    return false;
                }
                scrollTo(-distance, 0);
                break;
            case MotionEvent.ACTION_UP:
                // 当右滑距离超过item控件宽度的一半就将其移除,否则还原
                if (Math.abs(getScrollX()) > hideView.getWidth() / 2) {
                    scrollToEnd(mVelocityTracker, hideView.getWidth(), 200);
                } else {
                    scrollToEnd(mVelocityTracker, 0, 200);
                }
                isOpen = false;
                recycleVelocityTracker();
                break;
        }
        return true;
    }

    public void bindViewAndData(final View convertView,
                                final List list, final int position) {
        scrollTo(0, 0);
        this.list = list;
        this.convertView = convertView;
        this.convertView.setTag(convertView.getId(), this);
        setHideView();
        this.position = position;
        this.hideView.findViewById(R.id.button1).setOnClickListener(this);
        this.hideView.findViewById(R.id.button2).setOnClickListener(this);
        this.hideView.findViewById(R.id.button3).setOnClickListener(this);
    }

    public void scrollToEnd(VelocityTracker velocityTracker, int end,
                            int duration) {
        isActionUp = true;
        // 当前的偏移位置
        int scrollX = getScrollX();
        int velocityX = 0;
        if (velocityTracker != null) {
            velocityTracker.computeCurrentVelocity(1000);
            velocityX = (int) velocityTracker.getXVelocity();
        }
        int distance = 0;
        if (velocityX > 1000) {
            distance = -scrollX;
        } else if (velocityX < -1000) {
            distance = end - scrollX;
        } else {
            distance = end - scrollX;
        }
        mScroller.startScroll(scrollX, 0, distance, 0, duration);
        // 此时需要手动刷新View 否则没效果
        invalidate();<commit_msg>解决了一些滑动的冲突<commit_after>package cn.diaovision.omnicontrol.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Scroller;

import java.util.ArrayList;
import java.util.List;

import cn.diaovision.omnicontrol.R;
import cn.diaovision.omnicontrol.core.model.conference.Term;

/**
 * Created by TaoYimin on 2017/5/4.
 */
public class SlidingItemView extends RelativeLayout implements View.OnClickListener {
    private Scroller mScroller;

    private float downX, dispatchDownX, dispatchDownY;

    private boolean isOpen;
    private boolean canDrag = true;
    private boolean open = false;
    private boolean lastOpen = false;

    private VelocityTracker mVelocityTracker;

    private View convertView;

    private View hideView;

    private int position;

    private List<Term> list;

    public OnHideViewClickListener onHideViewClickListener;

    //手指是否up
    private boolean isActionUp = false;

    private int hideViewWidth;

    public SlidingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    public void setHideView() {
        if (hideView == null) {
            createHideView();
            if (convertView instanceof RelativeLayout) {
                ((RelativeLayout) convertView).addView(hideView);
                LayoutParams params = (LayoutParams) hideView
                        .getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                hideView.setLayoutParams(params);
                bringToFront();
            }
        }
    }

    private void createHideView() {
        hideView = LayoutInflater.from(getContext()).inflate(R.layout.layout_hide, this, false);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                dispatchDownX = event.getX();
                dispatchDownY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                if (Math.abs(dispatchDownX - event.getX()) < 10
                        && Math.abs(dispatchDownY - event.getY()) < 10) {
                    View parent = (View) convertView.getParent();
                    if (canRemove()) {
                        int width = hideViewWidth;
                        if (dispatchDownX < getWidth() - width) {
                            ViewGroup viewGroup = (ViewGroup) parent;
                            int count = viewGroup.getChildCount();
                            for (int i = 0; i < count; i++) {
                                View convertView = viewGroup.getChildAt(i);
                                SlidingItemView slidingItemView = (SlidingItemView) convertView
                                        .getTag(convertView.getId());
                                if (slidingItemView.canRemove()
                                        && slidingItemView != this) {
                                    slidingItemView.scrollToEnd(null, 0, 200);
                                }
                            }
                        }
                        return super.dispatchTouchEvent(event);
                    }

                    ViewGroup viewGroup = (ViewGroup) parent;
                    int count = viewGroup.getChildCount();
                    boolean isExistOpenItem = false;
                    for (int i = 0; i < count; i++) {
                        View convertView = viewGroup.getChildAt(i);
                        SlidingItemView slidingItemView = (SlidingItemView) convertView
                                .getTag(convertView.getId());
                        if (slidingItemView.canRemove()) {
                            isExistOpenItem = true;
                            slidingItemView.scrollToEnd(null, 0, 200);
                        }
                    }
                    if (!isExistOpenItem) {
                        convertView.performClick();
                    }
                    return false;
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        createVelocityTracker(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX() + getScrollX();
                if (canRemove()
                        && event.getX() > getWidth() - hideView.getWidth()) {
                    return false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int distance = (int) (event.getX() - downX);
                if (getScrollX() > hideView.getWidth() / 10) {
                    isOpen = true;
                }
                // 当左移,滑动操作被禁用
                if (distance > 0) {
                    scrollTo(0, 0);
                    return false;
                } else if (distance < -hideView.getWidth()) {
                    scrollTo(hideView.getWidth(), 0);
                    return false;
                }
                scrollTo(-distance, 0);
                break;
            case MotionEvent.ACTION_UP:
                // 当右滑距离超过item控件宽度的一半就将其移除,否则还原
                if (Math.abs(getScrollX()) > hideView.getWidth() / 2) {
                    scrollToEnd(mVelocityTracker, hideView.getWidth(), 200);
                } else {
                    scrollToEnd(mVelocityTracker, 0, 200);
                }
                isOpen = false;
                recycleVelocityTracker();
                break;
        }
        return true;
    }

    public void bindViewAndData(final View convertView,
                                final List list, final int position) {
        scrollTo(0, 0);
        this.list = list;
        this.convertView = convertView;
        this.convertView.setTag(convertView.getId(), this);
        setHideView();
        this.position = position;
        this.hideView.findViewById(R.id.button1).setOnClickListener(this);
        this.hideView.findViewById(R.id.button2).setOnClickListener(this);
        this.hideView.findViewById(R.id.button3).setOnClickListener(this);
    }

    public void scrollToEnd(VelocityTracker velocityTracker, int end,
                            int duration) {
        isActionUp = true;
        // 当前的偏移位置
        int scrollX = getScrollX();
        int velocityX = 0;
        if (velocityTracker != null) {
            velocityTracker.computeCurrentVelocity(1000);
            velocityX = (int) velocityTracker.getXVelocity();
        }
        int distance = 0;
        if (velocityX > 1000) {
            distance = -scrollX;
        } else if (velocityX < -1000) {
            distance = end - scrollX;
        } else {
            distance = end - scrollX;
        }
        mScroller.startScroll(scrollX, 0, distance, 0, duration);
        // 此时需要手动刷新View 否则没效果
        if (distance < 0) {
            if (open) {
                canDrag = false;
            } else {
                canDrag = true;
            }
            lastOpen = open;
            open = false;
        } else if (distance > 0) {
            canDrag = false;
            lastOpen = open;
            open = true;
        } else {
            lastOpen = open;
            open = !open;
            canDrag = false;
        }
        if (distance == -hideView.getWidth()) {
            canDrag = true;
        }
        //Log.i("info","distance="+distance+"上一次状态："+lastOpen+"这次状态:"+open+"能否拖拽："+canDrag);
        invalidate();
    }

    public boolean isCanDrag() {
        return canDrag;
    }

    public void setCanDrag(boolean canDrag) {
        this.canDrag = canDrag;
{'fraction_non_alphanumeric': 0.08958869743183469, 'fraction_numerical': 0.00765828216808609, 'mean_word_length': 1.4715287975199869, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6890666', 'n_tokens_mistral': 4103, 'n_tokens_neox': 3842, 'n_words': 805}