public class APPConst {
    private APPConst(){}

    // 设置ChannelManager频道管理中的每个item的间隔
    public static final int ITEM_SPACE = 5;

    // 0和1均表示ChannelManager频道管理中的tab不可可编辑
    // 其中tab的type为0时，字体会显示红色， 为1时会显示灰色
    public static final int ITEM_DEFAULT = 0;
    // 1均表示ChannelManager频道管理中的tab不可可编辑
    public static final int ITEM_UNEDIT = 1;
    // 表示ChannelManager频道管理中的tab可编辑
    public static final int ITEM_EDIT = 2;
} public class ItemDragHelperCallback extends ItemTouchHelper.Callback {
    private ItemDragListener mDragMoveListener;

    public ItemDragHelperCallback(ItemDragListener listener) {
        mDragMoveListener = listener;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return false;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager || layoutManager instanceof StaggeredGridLayoutManager) {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END;
        } else {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }
        int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
        if (source.getItemViewType() != target.getItemViewType())
            return false;
        mDragMoveListener.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
        mDragMoveListener.onItemSwiped(viewHolder.getAdapterPosition());
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            ItemDragVHListener itemViewHolder = (ItemDragVHListener) viewHolder;
            itemViewHolder.onItemSelected();
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        ItemDragVHListener itemViewHolder = (ItemDragVHListener) viewHolder;
        itemViewHolder.onItemFinished();
    }
} public class MyChannelHeaderWidget implements IChannelType {
    private RecyclerView mRecyclerView;
    private EditModeHandler editModeHandler;

    public MyChannelHeaderWidget(EditModeHandler handler) {
        this.editModeHandler = handler;
    }

    @Override
    public ChannelAdapter.ChannelViewHolder createViewHolder(LayoutInflater mInflater, ViewGroup parent) {
        mRecyclerView = (RecyclerView) parent;
        return new MyChannelHeaderViewHolder(mInflater.inflate(R.layout.activity_channel_my_header, parent, false));
    }

    @Override
    public void bindViewHolder(final ChannelAdapter.ChannelViewHolder holder, int position, ProjectChannelBean data) {
        final MyChannelHeaderViewHolder viewHolder = (MyChannelHeaderViewHolder) holder;
        // 右侧按键点击时改变样式，如编辑-》完成
        viewHolder.mEditModeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!viewHolder.mEditModeTv.isSelected()) {

                    if (editModeHandler != null)
                        editModeHandler.startEditMode(mRecyclerView);
                    viewHolder.mEditModeTv.setText("完成");
                } else {
                    if (editModeHandler != null)
                        editModeHandler.cancelEditMode(mRecyclerView);
                    viewHolder.mEditModeTv.setText("编辑");
                }
                viewHolder.mEditModeTv.setSelected(!viewHolder.mEditModeTv.isSelected());
            }
        });
    }

    public class MyChannelHeaderViewHolder extends ChannelAdapter.ChannelViewHolder {
        private TextView mEditModeTv;

        public MyChannelHeaderViewHolder(View itemView) {
            super(itemView);
            mEditModeTv = (TextView) itemView.findViewById(R.id.id_edit_mode);
        }
    }
} public class MyChannelWidget implements IChannelType {
    private RecyclerView mRecyclerView;
    private EditModeHandler editModeHandler;

    public MyChannelWidget(EditModeHandler editModeHandler) {
        this.editModeHandler = editModeHandler;
    }

    @Override
    public ChannelAdapter.ChannelViewHolder createViewHolder(LayoutInflater mInflater, ViewGroup parent) {
        mRecyclerView = (RecyclerView) parent;
        return new MyChannelHeaderViewHolder(mInflater.inflate(R.layout.activity_channel_my, parent, false));
    }

    @Override
    public void bindViewHolder(final ChannelAdapter.ChannelViewHolder holder, final int position, final ProjectChannelBean data) {
        final MyChannelHeaderViewHolder myHolder = (MyChannelHeaderViewHolder) holder;
        myHolder.mChannelTitleTv.setText(data.getTname());
        // 设置文字大小，通过判断tab中的文字长度，如果有4或者4个字以上则为16sp大小
        int textSize = data.getTname().length() >= 4 ? 14 : 16;
        myHolder.mChannelTitleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        // 通过channelbean的type值设置其文字背景颜色
        int type = data.getTabType();
        // 设置文字背景颜色
        myHolder.mChannelTitleTv.setBackgroundResource(type == APPConst.ITEM_DEFAULT || type == APPConst.ITEM_UNEDIT ?
                R.drawable.channel_fixed_bg_shape : R.drawable.channel_my_bg_shape);
        // 根据tab状态设置文字颜色
        myHolder.mChannelTitleTv.setTextColor(type == APPConst.ITEM_DEFAULT ? Color.RED :
                type == APPConst.ITEM_UNEDIT ? Color.parseColor("#666666") : Color.parseColor("#333333"));
        // 设置右上角删除按钮是否可见
        myHolder.mDeleteIv.setVisibility(data.getEditStatus() == 1 ? View.VISIBLE : View.INVISIBLE);
        myHolder.mChannelTitleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (editModeHandler != null && data.getTabType() == APPConst.ITEM_EDIT) {
                    editModeHandler.clickMyChannel(mRecyclerView, holder);
                }
            }
        });
        myHolder.mChannelTitleTv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (editModeHandler != null && data.getTabType() == APPConst.ITEM_EDIT) {
                    editModeHandler.touchMyChannel(motionEvent, holder);
                }
                return false;
            }
        });
        myHolder.mChannelTitleTv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (editModeHandler != null && data.getTabType() == APPConst.ITEM_EDIT) {
                    editModeHandler.clickLongMyChannel(mRecyclerView, holder);
                }
                return true;
            }
        });
    }

    public class MyChannelHeaderViewHolder extends ChannelAdapter.ChannelViewHolder {
        private TextView mChannelTitleTv;
        private ImageView mDeleteIv;

        private MyChannelHeaderViewHolder(View itemView) {
            super(itemView);
            mChannelTitleTv = (TextView) itemView.findViewById(R.id.id_channel_title);
            mDeleteIv = (ImageView) itemView.findViewById(R.id.id_delete_icon);
        }
    }
} public class ProjectChannelBean {
    private String topicid;
    // 设置该标签是否可编辑，如果出现在我的频道中，且值为1，则可在右上角显示删除按钮
    private int editStatus;
    private String cid;
    private String tname;
    private String ename;
    // 标签类型，显示是我的频道还是更多频道
    private int tabType;
    private String tid;
    private String column;

    public ProjectChannelBean(){}

    public ProjectChannelBean(String tname, String tid){
        this.tname = tname;
        this.tid = tid;
    }

    public ProjectChannelBean(String tname, String column, String tid){
        this.tname = tname;
        this.column = column;
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public int getTabType() {
        return tabType;
    }

    public void setTabType(int tabType) {
        this.tabType = tabType;
    }

    public int getEditStatus() {
        return editStatus;
    }

    public void setEditStatus(int editStatus) {
        this.editStatus = editStatus;
    }

    public String getTopicid() {
        return topicid;
    }

    public void setTopicid(String topicid) {
        this.topicid = topicid;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getTid() {
        return tid;
    }

    public void setTid(String tid) {
        this.tid = tid;
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }
} public class RecChannelHeaderWidget implements IChannelType {
    @Override
    public ChannelAdapter.ChannelViewHolder createViewHolder(LayoutInflater mInflater, ViewGroup parent) {
        return new MyChannelHeaderViewHolder(mInflater.inflate(R.layout.activity_channel_rec_header, parent, false));
    }

    @Override
    public void bindViewHolder(ChannelAdapter.ChannelViewHolder holder, int position, ProjectChannelBean data) {

    }

    public class MyChannelHeaderViewHolder extends ChannelAdapter.ChannelViewHolder {

        public MyChannelHeaderViewHolder(View itemView) {
            super(itemView);
        }
    }
} public class RecChannelWidget implements IChannelType {
    private EditModeHandler editModeHandler;
    private RecyclerView mRecyclerView;

    public RecChannelWidget(EditModeHandler editModeHandler) {
        this.editModeHandler = editModeHandler;
    }

    @Override
    public ChannelAdapter.ChannelViewHolder createViewHolder(LayoutInflater mInflater, ViewGroup parent) {
        this.mRecyclerView = (RecyclerView) parent;
        return new RecChannelHeaderViewHolder(mInflater.inflate(R.layout.activity_channel_rec, parent, false));
    }

    @Override
    public void bindViewHolder(final ChannelAdapter.ChannelViewHolder holder, int position, ProjectChannelBean data) {
        RecChannelHeaderViewHolder recHolder = (RecChannelHeaderViewHolder) holder;
        recHolder.mChannelTitleTv.setText(data.getTname());
        int textSize = data.getTname().length() >= 4 ? 14 : 16;
        recHolder.mChannelTitleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        recHolder.mChannelTitleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (editModeHandler != null) {
                    editModeHandler.clickRecChannel(mRecyclerView, holder);
                }
            }
        });
    }

    private class RecChannelHeaderViewHolder extends ChannelAdapter.ChannelViewHolder {
        private TextView mChannelTitleTv;

        private RecChannelHeaderViewHolder(View itemView) {
            super(itemView);
            mChannelTitleTv = (TextView) itemView.findViewById(R.id.id_channel_title);
        }
    }
} public abstract class EditModeHandler {
    public void startEditMode(RecyclerView mRecyclerView) {
    }

    public void cancelEditMode(RecyclerView mRecyclerView) {
    }

    public void clickMyChannel(RecyclerView mRecyclerView, ChannelAdapter.ChannelViewHolder holder) {
    }

    public void clickLongMyChannel(RecyclerView mRecyclerView, ChannelAdapter.ChannelViewHolder holder) {
    }

    public void touchMyChannel(MotionEvent motionEvent, ChannelAdapter.ChannelViewHolder holder) {
    }

    public void clickRecChannel(RecyclerView mRecyclerView, ChannelAdapter.ChannelViewHolder holder) {
    }
} public interface IChannelType {
    //我的频道头部部分
    int TYPE_MY_CHANNEL_HEADER = 0;
    //我的频道部分
    int TYPE_MY_CHANNEL = 1;
    //推荐头部部分
    int TYPE_REC_CHANNEL_HEADER = 2;
    //推荐部分
    int TYPE_REC_CHANNEL = 3;

    ChannelAdapter.ChannelViewHolder createViewHolder(LayoutInflater mInflater, ViewGroup parent);

    void bindViewHolder(ChannelAdapter.ChannelViewHolder holder, int position, ProjectChannelBean data);
} public interface ItemDragListener {
    void onItemMove(int fromPosition, int toPosition);

    void onItemSwiped(int position);
} public interface ItemDragVHListener {
    void onItemSelected();

    void onItemFinished();
}
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.channelmanager.APPConst;
import com.example.channelmanager.ItemDragHelperCallback;
import com.example.channelmanager.activity.MyChannelHeaderWidget;
import com.example.channelmanager.activity.MyChannelWidget;
import com.example.channelmanager.ProjectChannelBean;
import com.example.channelmanager.R;
import com.example.channelmanager.activity.RecChannelHeaderWidget;
import com.example.channelmanager.activity.RecChannelWidget;
import com.example.channelmanager.base.EditModeHandler;
import com.example.channelmanager.base.IChannelType;
import com.example.channelmanager.base.ItemDragListener;
import com.example.channelmanager.base.ItemDragVHListener;
import java.util.List;
package com.example.channelmanager.adapter;




/**
 * Created by goach on 2016/9/28.
 */

public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> implements
        ItemDragListener {
    private List<ProjectChannelBean> mMyChannelItems;
    private List<ProjectChannelBean> mOtherChannelItems;
    private int mMyHeaderCount;
    private int mRecHeaderCount;
    private LayoutInflater mInflater;
    private SparseArray<IChannelType> mTypeMap = new SparseArray();
    private boolean isEditMode;
    private ItemTouchHelper mItemTouchHelper;
    // touch 点击开始时间
    private long startTime;
    // touch 间隔时间  用于分辨是否是 "点击"
    private static final long SPACE_TIME = 100;
    private ChannelItemClickListener channelItemClickListener;


    public ChannelAdapter(Context context, RecyclerView recyclerView, List<ProjectChannelBean> myChannelItems, List<ProjectChannelBean> otherChannelItems,
                          int myHeaderCount, int recHeaderCount) {
        this.mItemTouchHelper = new ItemTouchHelper(new ItemDragHelperCallback(this));
        mItemTouchHelper.attachToRecyclerView(recyclerView);
        this.mMyChannelItems = myChannelItems;
        this.mOtherChannelItems = otherChannelItems;
        this.mMyHeaderCount = myHeaderCount;
        this.mRecHeaderCount = recHeaderCount;
        mInflater = LayoutInflater.from(context);
        mTypeMap.put(IChannelType.TYPE_MY_CHANNEL_HEADER, new MyChannelHeaderWidget(new EditHandler()));
        mTypeMap.put(IChannelType.TYPE_MY_CHANNEL, new MyChannelWidget(new EditHandler()));
        mTypeMap.put(IChannelType.TYPE_REC_CHANNEL_HEADER, new RecChannelHeaderWidget());