public class SLIndexPath {
    private int row;
    private int section;

    public SLIndexPath() {
    }

    public SLIndexPath(int section,int row) {
        if (row < 0) row = 0;
        if (section < 0) section = 0;
        this.row = row;
        this.section = section;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        if (row < 0) row = 0;
        this.row = row;
    }

    public int getSection() {
        return section;
    }

    public void setSection(int section) {
        if (section < 0) section = 0;
        this.section = section;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)return false;
        SLIndexPath indexPath = (SLIndexPath) obj;
        return this.getRow() == indexPath.getRow() && this
                .getSection() == indexPath.getSection();
    }

    @Override
    public SLIndexPath clone() {
        return new SLIndexPath(getSection(),getRow());
    }

    protected String key(){
        return section+":"+row;
    }

    @Override
    public String toString() {
        return "SLIndexPath[section:"+section+" row:"+row+"]";
    }
} public class SLTableView extends RecyclerView {

    private SLTableViewDataSource tableViewDataSource;
    private SLTableViewDelegate tableViewDelegate;
    private SLTableViewAdapter tableViewAdapter;

    private int bgColor;

    public SLTableView(Context context) {
        super(context);
    }

    public void setTableViewDataSource(SLTableViewDataSource tableViewDataSource) {
        this.tableViewDataSource = tableViewDataSource;
    }

    public void setTableViewDelegate(SLTableViewDelegate tableViewDelegate) {
        this.tableViewDelegate = tableViewDelegate;
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        setBackgroundColor(bgColor);
    }

    public void notifyDataSetChanged() {
        this.getRecycledViewPool().clear();
        this.getTableViewAdapter().notifyDataSetChanged();
    }

    public void notifyDataSetChanged(int section, int row) {
        this.getTableViewAdapter().notifyDataSetChanged(section, row);
    }

    public SLTableViewAdapter getTableViewAdapter() {
        return tableViewAdapter;
    }

    public void setTableViewAdapter(SLTableViewAdapter tableViewAdapter) {
        this.tableViewAdapter = tableViewAdapter;
        this.setAdapter(tableViewAdapter);
    }

    public void scrollToRowAtIndexPath(SLIndexPath indexPath) {
        this.tableViewAdapter.scrollToRowAtIndexPath(indexPath);
    }

    private HashMap<String, Object> mKeyValues = new HashMap<>();

    public HashMap<String, Object> keyValues() {
        mKeyValues.putAll(mKeyValues());
        return mKeyValues;
    }


    private HashMap<String, Object> mKeyValues() {
        HashMap<String, Object> resMaps = new HashMap<>();
        int count = this.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = this.getChildAt(i);
            ViewHolder holder = this.getChildViewHolder(view);
            if (null != holder) {
                SLTableViewCell cell = (SLTableViewCell) holder;
                HashMap<String, Object> cellKeyValues = cell.keyValues();
                if (null != cellKeyValues) {
                    resMaps.putAll(cellKeyValues);
                }

            }

        }
        return resMaps;
    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        mKeyValues.putAll(mKeyValues());
    }
//
//    /**
//     * 获取cell中的值
//     * @param values 传入map对象
//     * @return true 获取完整,false 有值不完整
//     */
//    public boolean keyValues(HashMap<String,Object> values){
//        int count = getChildCount();
//        for (int i = 0; i < count; i++) {
//            ViewHolder holder = getChildViewHolder(getChildAt(i));
//            if (holder instanceof SLTableViewCell){
//                SLTableViewCell cell = (SLTableViewCell) holder;
//                boolean isRequired = cell.isRequiredValue();
//                HashMap<String, Object> cellValues = cell.keyValues();
//                if (null != cellValues && !cellValues.isEmpty()){
//                    if (isRequired && !checkValues(cell,cellValues)){
//                        animateAndScroll(cell);
//                        notifyValueError(cell,cellValues);
//                        return false;
//                    }
//                    values.putAll(cellValues);
//                }else{
//                    if (isRequired) {
//                        animateAndScroll(cell);
//                        notifyValueError(cell,cellValues);
//                        return false;
//                    }
//                }
//            }
//        }
//        return true;
//    }
//
//    private boolean checkValues(SLTableViewCell cell,HashMap<String,Object> values){
//        Set<String> keySet = values.keySet();
//        for (String s : keySet) {
//            Object object = values.get(s);
//            if (!cell.valueFilter(object)) {
//                return false;
//            }
//        }
//        return true;
//    }
//
//    private void notifyValueError(SLTableViewCell cell,HashMap<String,Object> value){
//        if (null != this.tableViewDelegate) {
//            this.tableViewDelegate.onCellValueError(this,cell.getIndexPath(),value);
//        }
//    }
//
//    private void animateAndScroll(SLTableViewCell cell){
//        SLIndexPath indexPath = cell.getIndexPath();
//        this.scrollToRowAtIndexPath(indexPath);
//        Animation animation = cell.animationWithNullValue();
//        if (null != animation) {
//            cell.itemView.startAnimation(animation);
//        }
//    }

    public static class Builder {

        private Context context;
        private SLTableView tableView;
        private LayoutParams layoutParams;

        private SLTableViewDataSource tableViewDataSource;
        private SLTableViewDelegate tableViewDelegate;
        private SLTableViewLayoutManagerExpand tableViewLayoutManagerExpand;
        private LayoutManager layoutManager;
//        private LinearLayoutManager linearLayoutManager;
//        private GridLayoutManager gridLayoutManager;

        private boolean stickyHeader;

        private int bgColor = 0;

        private int headerBgColor;
        private int headerTextColor;
        private float headerTextSize;

        private int floorBgColor;
        private int floorTextColor;
        private float floorTextSize;

        private boolean autoAddEmptyGridItem = true;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置{@link SLTableView}的数据源
         * <p>
         * 必须设置,不然不会显示列表.
         *
         * @param tableViewDataSource {@link SLTableViewDataSource}
         * @return {@link Builder}
         */
        public Builder setTableViewDataSource(SLTableViewDataSource tableViewDataSource) {
            this.tableViewDataSource = tableViewDataSource;
            return this;
        }

        /**
         * 设置{@link SLTableView}的扩展项
         *
         * @param tableViewDelegate {@link SLTableViewDelegate}
         * @return {@link Builder}
         */
        public Builder setTableViewDelegate(SLTableViewDelegate tableViewDelegate) {
            this.tableViewDelegate = tableViewDelegate;
            return this;
        }

        public Builder setLayoutParams(LayoutParams layoutParams) {
            this.layoutParams = layoutParams;
            return this;
        }

        /**
         * 设置{@link SLTableView}背景色
         *
         * @param bgColor
         * @return {@link Builder}
         */
        public Builder setBgColor(int bgColor) {
            this.bgColor = bgColor;
            return this;
        }

        /**
         * 设置每组的header是否浮动.
         *
         * @param stickyHeader true 浮动, false 不浮动
         * @return {@link Builder}
         */
        public Builder showStickyHeader(boolean stickyHeader) {
            this.stickyHeader = stickyHeader;
            return this;
        }

        /**
         * 设置header背景色
         *
         * @param headerBgColor 颜色值
         * @return {@link Builder}
         */
        public Builder setHeaderBgColor(int headerBgColor) {
            this.headerBgColor = headerBgColor;
            return this;
        }

        /**
         * 设置header文字颜色
         *
         * @param headerTextColor 颜色值
         * @return {@link Builder}
         */
        public Builder setHeaderTextColor(int headerTextColor) {
            this.headerTextColor = headerTextColor;
            return this;
        }

        /**
         * 设置header文字大小
         *
         * @param headerTextSize 颜色值
         * @return {@link Builder}
         */
        public Builder setHeaderTextSize(float headerTextSize) {
            this.headerTextSize = headerTextSize;
            return this;
        }

        /**
         * 设置floor背景色
         *
         * @param floorBgColor 颜色值
         * @return {@link Builder}
         */
        public Builder setFloorBgColor(int floorBgColor) {
            this.floorBgColor = floorBgColor;
            return this;
        }

        /**
         * 设置floor文字色
         *
         * @param floorTextColor 颜色值
         * @return {@link Builder}
         */
        public Builder setFloorTextColor(int floorTextColor) {
            this.floorTextColor = floorTextColor;
            return this;
        }

        /**
         * 设置floor文字大小
         *
         * @param floorTextSize 颜色值
         * @return {@link Builder}
         */
        public Builder setFloorTextSize(float floorTextSize) {
            this.floorTextSize = floorTextSize;
            return this;
        }

        @Deprecated
        public Builder setLayoutManager(LayoutManager layoutManager) {
            if (layoutManager instanceof GridLayoutManager) {
                useGridLayoutManager(((GridLayoutManager) layoutManager).getSpanCount());
            } else {
                useLinearLayoutManager();
            }
            return this;
        }

        public Builder useLinearLayoutManager() {
            layoutManager = new SLLinearLayoutManager(context);
            return this;
        }

        public Builder useGridLayoutManager(int spanCount) {
            layoutManager = new SLGridLayoutManager(context, spanCount);
            return this;
        }

        /**
         * 和 Builder#setLayoutManager(LayoutManager)  GridLayoutManager一起使用
         *
         * @param tableViewLayoutManagerExpand {@link SLTableViewLayoutManagerExpand}
         * @return {@link Builder}
         */
        public Builder setTableViewLayoutManagerExpand(SLTableViewLayoutManagerExpand tableViewLayoutManagerExpand) {
            this.tableViewLayoutManagerExpand = tableViewLayoutManagerExpand;
            return this;
        }

        public Builder setAutoAddEmptyGridItem(boolean autoAddEmptyGridItem) {
            this.autoAddEmptyGridItem = autoAddEmptyGridItem;
            return this;
        }

        public SLTableView build() {
            if (tableView == null) {
                tableView = new SLTableView(context);
            }
            if (layoutParams != null) {
                tableView.setLayoutParams(layoutParams);
            } else {
                tableView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            if (bgColor != 0) {
                tableView.setBgColor(bgColor);
            } else {
                tableView.setBgColor(context.getResources().getColor(R.color.color_title_floor_background));
            }
            tableView.setTableViewDataSource(tableViewDataSource);
            tableView.setTableViewDelegate(tableViewDelegate);
            if (layoutManager != null) {
                tableView.setLayoutManager(layoutManager);
            } else {
                tableView.setLayoutManager(new SLLinearLayoutManager(context));
            }
            SLTableViewAdapter adapter = null;
            if (!stickyHeader) {
                adapter = new SLTableViewAdapter(context, tableView, tableViewDataSource, tableViewDelegate, tableViewLayoutManagerExpand);
                SLItemDecoration decoration = new SLItemDecoration(adapter);
                tableView.setTableViewAdapter(adapter);
                tableView.addItemDecoration(decoration);
            } else {
                adapter = new SLTableViewStickyAdapter(context, tableView, tableViewDataSource, tableViewDelegate, tableViewLayoutManagerExpand);
                SLStickyHeaderDecoration decoration = new SLStickyHeaderDecoration((SLTableViewStickyAdapter) adapter);
                tableView.setTableViewAdapter(adapter);
                tableView.addItemDecoration(decoration);
                StickyRecyclerHeadersTouchListener listener = new StickyRecyclerHeadersTouchListener(tableView, decoration);
                tableView.addOnItemTouchListener(listener);
            }
            adapter.setAutoAddEmptyGridItem(this.autoAddEmptyGridItem);
            if (layoutManager instanceof GridLayoutManager
                    && ((GridLayoutManager) layoutManager).getSpanSizeLookup() instanceof GridLayoutManager.DefaultSpanSizeLookup) {
                ((GridLayoutManager) layoutManager).setSpanSizeLookup(new SLDefaultSpanSizeLookup(adapter, (GridLayoutManager) layoutManager));
            }
            int bgcolor = context.getResources().getColor(R.color.color_background);
            int textcolor = context.getResources().getColor(R.color.color_text_grey);
            float textsize = context.getResources().getDimension(R.dimen.dimen_cell_header_floor);
            adapter.setHeaderBgColor(headerBgColor != 0 ? headerBgColor : bgcolor);
            adapter.setHeaderTextColor(headerTextColor != 0 ? headerTextColor : textcolor);
            adapter.setHeaderTextSize(headerTextSize != 0 ? headerTextSize : textsize);

            adapter.setFloorBgColor(floorBgColor != 0 ? floorBgColor : bgcolor);
            adapter.setFloorTextColor(floorTextColor != 0 ? floorTextColor : textcolor);
            adapter.setFloorTextSize(floorTextSize != 0 ? floorTextSize : textsize);

            return tableView;
        }
    }

    private static class SLDefaultSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
        private SLTableViewExpandAdapter spanSizeLookup;
        private GridLayoutManager layoutManager;

        public SLDefaultSpanSizeLookup(SLTableViewExpandAdapter spanSizeLookup, GridLayoutManager layoutManager) {
            this.spanSizeLookup = spanSizeLookup;
            this.layoutManager = layoutManager;
        }

        @Override
        public int getSpanSize(int position) {
            if (spanSizeLookup.headerFloorOfPosition(position)) {
                return layoutManager.getSpanCount();
            } else {
                int size = spanSizeLookup.getSpanSize(position);
                return size > 0 ? size : 1;
            }
        }
    }

} public class SLTableViewCell extends RecyclerView.ViewHolder implements View.OnClickListener{

    private SLIndexPath indexPath;
    private SLCellViewClickListener clickListener;
    private Object userData;
    private String key;
    private boolean requiredValue;
    private ValueFilter valueFilter;

    public SLTableViewCell(View itemView) {
        super(itemView);
    }

    /**
     *
     * @param view 需要设置点击监听的view
     * @param clickListener 回调接口
     */
    public void bindCellViewClick(View view,SLCellViewClickListener clickListener){
        bindCellViewClick(view,clickListener,null);
    }

    /**
     *
     * @param view 需要设置点击监听的view
     * @param clickListener 回调接口
     * @param userData 用户数据
     */
    public void bindCellViewClick(View view,SLCellViewClickListener clickListener,Object userData){
        view.setOnClickListener(this);
        this.clickListener = clickListener;
        this.userData = userData;
    }

    public SLTableViewCell setIndexPath(SLIndexPath indexPath) {
        this.indexPath = indexPath;
        return this;
    }


    public SLIndexPath getIndexPath() {
        return indexPath.clone();
    }

    @Override
    public void onClick(View v) {
        if (clickListener != null){
            clickListener.onCellViewClick(v,indexPath,userData);
        }
    }

    public boolean isRequiredValue() {
        return requiredValue;
    }

    public SLTableViewCell setRequiredValue(boolean requiredValue) {
        this.requiredValue = requiredValue;
        return this;
    }

    public String getKey() {
        return key;
    }

    public SLTableViewCell setKey(String key) {
        this.key = key;
        return this;
    }

    public HashMap<String,Object> keyValues(){
        HashMap<String,Object> result = new HashMap<String,Object>();
        String key = getKey();
        Object value = value();
        if (!TextUtils.isEmpty(key) && null != value){
            result.put(key,value);
        }
        return result;
    }

//    public ValueFilter getValueFilter() {
//        return valueFilter;
//    }
//
//    public SLTableViewCell setValueFilter(ValueFilter valueFilter) {
//        this.valueFilter = valueFilter;
//        return this;
//    }

//    @Override
//    public boolean valueFilter(Object value){
//        if (null != getValueFilter()) {
//            return getValueFilter().valueFilter(value);
//        } else if (null == value) {
//            return false;
//        } else if (value instanceof String) {
//            return ((String)value).length() > 0;
//        }
//        return false;
//    }

    /**
     *
     * @return
     */
    public Object value(){
        return null;
    }

//    /**
//     * 当 requiredValue = true 和 value() 是空或者长度为0时, 显示的动画
//     * @return
//     */
//    public Animation animationWithNullValue(){
//        TranslateAnimation animation = new TranslateAnimation(-20, 20, 0, 0);
//        animation.setInterpolator(new AccelerateInterpolator());
//        animation.setDuration(100);
//        animation.setRepeatCount(3);
//        animation.setRepeatMode(Animation.REVERSE);
//        return animation;
//    }

    public  interface SLCellViewClickListener{
        public void onCellViewClick(View view, SLIndexPath indexPath, Object userData);
    }

} public interface SLTableViewDataSource {

    /**
     * 有多少组
     * @param tableView {@link SLTableView}
     * @return 组数
     */
    public int numberOfSections(SLTableView tableView);

    /**
     * 每组多少列
     * @param tableView  {@link SLTableView}
     * @param section 组下标
     * @return 列数
     */
    public int numberOfRowsInSection(SLTableView tableView, int section);

    /**
     * 返回当前位置的类型[0,n]
     * @param tableView {@link SLTableView}
     * @param indexPath 组和对应的列
     * @return 类型
     */
    public int typeOfIndexPath(SLTableView tableView, SLIndexPath indexPath);

    /**
     *
     * 根据类型返回 cell
     *
     * @param tableView {@link SLTableView}
     * @param parent {@link ViewGroup}
     * @param type 类型
     * @return 返回 {@link SLTableViewCell}
     */
    public SLTableViewCell cellForType(SLTableView tableView, ViewGroup parent, int type);

    /**
     * 绑定数据
     * @param tableView {@link SLTableView}
     * @param cell  {@link SLTableViewCell}
     * @param indexPath 组和对应的列
     * @param type 类型
     */
    public void onBindCell(SLTableView tableView, SLTableViewCell cell, SLIndexPath indexPath, int type);

} public interface SLTableViewDelegate {

    /**
     * section组的title 描述
     * @param tableView SLTableView
     * @param section 组下标
     * @return header
     */
    public String titleForHeaderInSection(SLTableView tableView, int section);

    /**
     * section组的floor 描述
     * @param tableView {@link SLTableView}
     * @param section 组下标
     * @return header
     */
    public String titleForFooterInSection(SLTableView tableView, int section);

    /**
     * section组title是否隐藏
     * @param tableView {@link SLTableView}
     * @param section 组下标
     * @return true 隐藏,false不隐藏
     */
    public boolean hiddenHeaderInSection(SLTableView tableView, int section);

    /**
     * section组的floor是否隐藏
     * @param tableView {@link SLTableView}
     * @param section 组下标
     * @return true 隐藏,false不隐藏
     */
    public boolean hiddenFooterInSection(SLTableView tableView, int section);


    /**
     * 自定义每组header
     * @param tableView  {@link SLTableView}
     * @param section 组数
     * @return {@link View}
     */
    public View viewForHeaderInSection(SLTableView tableView, int section);

    /**
     * 自定义每组footer
     * @param tableView  {@link SLTableView}
     * @param section 组数
     * @return {@link View}
     */
    public View viewForFooterInSection(SLTableView tableView,int section);



    /**
     * 绑定自定义每组header的数据
     * @param tableView  {@link SLTableView}
     * @param view {@link View}
     * @param section 组数
     */
    public void onBindHeaderInSection(SLTableView tableView, View view, int section);

    /**
     * 自定义每组footer
     * @param tableView  {@link SLTableView}
     * @param view {@link View}
     * @param section 组数
     */
    public void onBindFooterInSection(SLTableView tableView, View view,int section);


    /**
     * cell中某个值错误
     * @param tableView {@link SLTableView}
     * @param indexPath 位置
     * @param value 某个值
     */
    public void onCellValueError(SLTableView tableView, SLIndexPath indexPath, Object value);

//    public boolean canEditRowAtIndexPath(SLTableView tableView, SLIndexPath indexPath);
//
//    public boolean canMoveRowAtIndexPath(SLTableView tableView, SLIndexPath indexPath);
//
//    public void moveRow(SLTableView tableView, SLIndexPath fromIndexPath, SLIndexPath toIndexPath);
} public interface SLTableViewLayoutManagerExpand {
    /**
     *
     * 使用GridLayoutManager时,设置某一行跨度
     *
     * @param indexPath {@link SLIndexPath} 位置
     * @return default 0
     */
    public int gridSpanSizeOfIndexPath(SLIndexPath indexPath);

    /**
     * 设置grid间距
     * @param outRect rect
     * @param indexPath 位置
     */
    void getItemOffsets(Rect outRect, SLIndexPath indexPath);
} public class ButtonBean extends CellBaseBean {

    private String text;

    public ButtonBean() {
        super(CellType.CELL_TYPE_BUTTON);
    }

    public String getText() {
        return text;
    }

    public ButtonBean setText(String text) {
        this.text = text;
        return this;
    }
} public class CellBaseBean {
    private CellType type;
    private int spanSize;
    private String key;
    private boolean requiredValue;

    public CellBaseBean(CellType type) {
        this(type, 1);
    }

    /**
     * @param type     显示的类型
     * @param spanSize grid 中一行跨越的大小
     */
    public CellBaseBean(CellType type, int spanSize) {
        this.type = type;
        this.spanSize = spanSize;
    }

    public CellType getType() {
        return type;
    }

    public int getSpanSize() {
        return spanSize;
    }

    public CellBaseBean setSpanSize(int spanSize) {
        this.spanSize = spanSize;
        return this;
    }

    public String getKey() {
        return key;
    }

    public CellBaseBean setKey(String key) {
        this.key = key;
        return this;
    }

    public boolean isRequiredValue() {
        return requiredValue;
    }

    public CellBaseBean setRequiredValue(boolean requiredValue) {
        this.requiredValue = requiredValue;
        return this;
    }

} public enum CellType {
    CELL_TYPE_DOUYU_HEAD(0,"头类型"),
    CELL_TYPE_DOUYU_ROOM(1,"一般房间类型"),
    CELL_TYPE_DOUYU_HOT_AUTHOR(2,"热门作者类型"),
    CELL_TYPE_DOUYU_ROOM_YANZHI(3,"颜值类型"),

    CELL_TYPE_TEXT(1000,"单纯文本类型"),
    CELL_TYPE_GENDER_SELECTOR(1001,"性别选择"),
    CELL_TYPE_EDIT_TEXT(1002,"输入文本类型"),
    CELL_TYPE_CHECK_BOX(1003,"右侧单选类型"),
    CELL_TYPE_EDIT_TEXT_UNIT(1004,"右侧带单位的输入框"),
    CELL_TYPE_DATE_SELECTOR(1005,"时间选择类型"),
    CELL_TYPE_BUTTON(1006,"按钮"),
    CELL_TYPE_LINE(1007,"分割线"),
    ;
    int cellType;
    String desc;

    CellType(int cellType, String desc) {
        this.cellType = cellType;
        this.desc = desc;
    }

    public int getCellType() {
        return cellType;
    }

    public CellType setCellType(int cellType) {
        this.cellType = cellType;
        return this;
    }

    public String getDesc() {
        return desc;
    }

    public CellType setDesc(String desc) {
        this.desc = desc;
        return this;
    }

    public static CellType typeOfCellType(int cellType){
        CellType type = null;
        CellType[] types = values();
        for (CellType douyuType : types) {
            if (douyuType.getCellType() == cellType){
                type = douyuType;
                break;
            }
        }
        return type;
    }
} public class CheckBoxBean extends CellBaseBean {

    private int icon;
    private String content;
    private String select;//除了 null 和 "0" 都是选择

    public CheckBoxBean() {
        super(CellType.CELL_TYPE_CHECK_BOX);
    }

    public int getIcon() {
        return icon;
    }

    public CheckBoxBean setIcon(int icon) {
        this.icon = icon;
        return this;
    }

    public String getContent() {
        return content;
    }

    public CheckBoxBean setContent(String content) {
        this.content = content;
        return this;
    }

    public String getSelect() {
        return select;
    }

    public CheckBoxBean setSelect(String select) {
        this.select = select;
        return this;
    }
} public class DateSelectorBean extends CellBaseBean {
    private int icon;
    private String content;
    private String date;

    public DateSelectorBean() {
        super(CellType.CELL_TYPE_DATE_SELECTOR);
    }

    public int getIcon() {
        return icon;
    }

    public DateSelectorBean setIcon(int icon) {
        this.icon = icon;
        return this;
    }

    public String getContent() {
        return content;
    }

    public DateSelectorBean setContent(String content) {
        this.content = content;
        return this;
    }

    public String getDate() {
        return date;
    }

    public DateSelectorBean setDate(String date) {
        this.date = date;
        return this;
    }
} public class DouyuHeadBean extends CellBaseBean {

    private Drawable headIcon;
    private String title;
    private String more;

    public DouyuHeadBean() {
        super(CellType.CELL_TYPE_DOUYU_HEAD,2);
    }

    public DouyuHeadBean(String title) {
        super(CellType.CELL_TYPE_DOUYU_HEAD,2);
        this.title = title;
    }

    public Drawable getHeadIcon() {
        return headIcon;
    }

    public DouyuHeadBean setHeadIcon(Drawable headIcon) {
        this.headIcon = headIcon;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public DouyuHeadBean setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getMore() {
        return more;
    }

    public DouyuHeadBean setMore(String more) {
        this.more = more;
        return this;
    }
} public class DouyuHotAuthorBean extends CellBaseBean {
    private Drawable headIcon;
    private String authorName;
    private String videoNumber;
    private String subscribeNumber;

    public DouyuHotAuthorBean() {
        super(CellType.CELL_TYPE_DOUYU_HOT_AUTHOR,2);
    }

    public DouyuHotAuthorBean(String authorName, String videoNumber, String subscribeNumber) {
        super(CellType.CELL_TYPE_DOUYU_HOT_AUTHOR,2);
        this.authorName = authorName;
        this.videoNumber = videoNumber;
        this.subscribeNumber = subscribeNumber;
    }



    public Drawable getHeadIcon() {
        return headIcon;
    }

    public DouyuHotAuthorBean setHeadIcon(Drawable headIcon) {
        this.headIcon = headIcon;
        return this;
    }

    public String getAuthorName() {
        return authorName;
    }

    public DouyuHotAuthorBean setAuthorName(String authorName) {
        this.authorName = authorName;
        return this;
    }

    public String getVideoNumber() {
        return videoNumber;
    }

    public DouyuHotAuthorBean setVideoNumber(String videoNumber) {
        this.videoNumber = videoNumber;
        return this;
    }

    public String getSubscribeNumber() {
        return subscribeNumber;
    }

    public DouyuHotAuthorBean setSubscribeNumber(String subscribeNumber) {
        this.subscribeNumber = subscribeNumber;
        return this;
    }
} public class DouyuRoomBean extends CellBaseBean {

    private String roomId;
    private String roomOwner;
    private String roomTitle;
    private String roomNumber;
    private Drawable roomImage;


    public DouyuRoomBean() {
        super(CellType.CELL_TYPE_DOUYU_ROOM);
    }

    public DouyuRoomBean(String roomOwner, String roomTitle, String roomNumber) {
        super(CellType.CELL_TYPE_DOUYU_ROOM);
        this.roomOwner = roomOwner;
        this.roomTitle = roomTitle;
        this.roomNumber = roomNumber;
    }



    public String getRoomId() {
        return roomId;
    }

    public DouyuRoomBean setRoomId(String roomId) {
        this.roomId = roomId;
        return this;
    }

    public String getRoomOwner() {
        return roomOwner;
    }

    public DouyuRoomBean setRoomOwner(String roomOwner) {
        this.roomOwner = roomOwner;
        return this;
    }

    public String getRoomTitle() {
        return roomTitle;
    }

    public DouyuRoomBean setRoomTitle(String roomTitle) {
        this.roomTitle = roomTitle;
        return this;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public DouyuRoomBean setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
        return this;
    }

    public Drawable getRoomImage() {
        return roomImage;
    }

    public DouyuRoomBean setRoomImage(Drawable roomImage) {
        this.roomImage = roomImage;
        return this;
    }
} public class DouyuYanzhiBean extends CellBaseBean {

    private String roomId;
    private String roomOwner;
    private String roomName;
    private String roomNumber;
    private String location;
    private Drawable roomImage;

    public DouyuYanzhiBean() {
        super(CellType.CELL_TYPE_DOUYU_ROOM_YANZHI);
    }

    public DouyuYanzhiBean(String roomOwner, String roomNumber, String location) {
        super(CellType.CELL_TYPE_DOUYU_ROOM_YANZHI);
        this.roomOwner = roomOwner;
        this.roomNumber = roomNumber;
        this.location = location;
    }



    public String getRoomId() {
        return roomId;
    }

    public DouyuYanzhiBean setRoomId(String roomId) {
        this.roomId = roomId;
        return this;
    }

    public String getRoomOwner() {
        return roomOwner;
    }

    public DouyuYanzhiBean setRoomOwner(String roomOwner) {
        this.roomOwner = roomOwner;
        return this;
    }

    public String getRoomName() {
        return roomName;
    }

    public DouyuYanzhiBean setRoomName(String roomName) {
        this.roomName = roomName;
        return this;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public DouyuYanzhiBean setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
        return this;
    }

    public String getLocation() {
        return location;
    }

    public DouyuYanzhiBean setLocation(String location) {
        this.location = location;
        return this;
    }

    public Drawable getRoomImage() {
        return roomImage;
    }

    public DouyuYanzhiBean setRoomImage(Drawable roomImage) {
        this.roomImage = roomImage;
        return this;
    }
} public class EditTextBean extends CellBaseBean {
    private int icon;
    private String hint;
    private String content;
    private int inputType = EditorInfo.TYPE_CLASS_TEXT; // 默认text

    public EditTextBean() {
        super(CellType.CELL_TYPE_EDIT_TEXT);
    }

    public int getIcon() {
        return icon;
    }

    public EditTextBean setIcon(int icon) {
        this.icon = icon;
        return this;
    }

    public String getHint() {
        return hint;
    }

    public EditTextBean setHint(String hint) {
        this.hint = hint;
        return this;
    }

    public String getContent() {
        return content;
    }

    public EditTextBean setContent(String content) {
        this.content = content;
        return this;
    }

    public int getInputType() {
        return inputType;
    }

    public EditTextBean setInputType(int inputType) {
        this.inputType = inputType;
        return this;
    }
} public class EditTextUnitBean extends CellBaseBean {

    private int icon;
    private String hint;
    private String content;
    private String unit;
    private int inputType = EditorInfo.TYPE_CLASS_TEXT; // 默认text

    public EditTextUnitBean() {
        super(CellType.CELL_TYPE_EDIT_TEXT_UNIT);
    }

    public int getIcon() {
        return icon;
    }

    public EditTextUnitBean setIcon(int icon) {
        this.icon = icon;
        return this;
    }

    public String getHint() {
        return hint;
    }

    public EditTextUnitBean setHint(String hint) {
        this.hint = hint;
        return this;
    }

    public String getContent() {
        return content;
    }

    public EditTextUnitBean setContent(String content) {
        this.content = content;
        return this;
    }

    public String getUnit() {
        return unit;
    }

    public EditTextUnitBean setUnit(String unit) {
        this.unit = unit;
        return this;
    }

    public int getInputType() {
        return inputType;
    }

    public EditTextUnitBean setInputType(int inputType) {
        this.inputType = inputType;
        return this;
    }
} public class GenderSelectorBean extends CellBaseBean {
    private int icon;
    private String gender;
    private String maleStr;
    private String femaleStr;

    public GenderSelectorBean() {
        super(CellType.CELL_TYPE_GENDER_SELECTOR);
    }

    public int getIcon() {
        return icon;
    }

    public GenderSelectorBean setIcon(int icon) {
        this.icon = icon;
        return this;
    }

    public String getGender() {
        return gender;
    }

    public GenderSelectorBean setGender(String gender) {
        this.gender = gender;
        return this;
    }

    public String getMaleStr() {
        return maleStr;
    }

    public GenderSelectorBean setMaleStr(String maleStr) {
        this.maleStr = maleStr;
        return this;
    }

    public String getFemaleStr() {
        return femaleStr;
    }

    public GenderSelectorBean setFemaleStr(String femaleStr) {
        this.femaleStr = femaleStr;
        return this;
    }
} public class TextBean extends CellBaseBean {
    private int icon;
    private String content;
    public TextBean() {
        super(CellType.CELL_TYPE_TEXT);
    }

    public int getIcon() {
        return icon;
    }

    public TextBean setIcon(int icon) {
        this.icon = icon;
        return this;
    }

    public String getContent() {
        return content;
    }

    public TextBean setContent(String content) {
        this.content = content;
        return this;
    }
} public class ButtonCell extends SLTableViewCell {
    public final LinearLayout ll_root;
    public final TextView tv_text;
    public ButtonCell(View itemView) {
        super(itemView);
        ll_root = (LinearLayout) itemView.findViewById(R.id.ll_root);
        tv_text = (TextView) itemView.findViewById(R.id.tv_text);
    }
} public class CheckBoxCell extends SLTableViewCell {

    public final ImageView iv_icon;
    public final TextView tv_content;
    public final CheckBox cb_check;

    public CheckBoxCell(View itemView) {
        super(itemView);
        iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        tv_content = (TextView) itemView.findViewById(R.id.tv_content);
        cb_check = (CheckBox) itemView.findViewById(R.id.cb_check);
    }

    @Override
    public Object value() {
        return cb_check.isChecked() ? "1" : "0";
    }
} public class DateSelectorCell extends SLTableViewCell {

    public final ImageView iv_icon;
    public final TextView tv_content;
    public final TextView tv_date;
    public final LinearLayout ll_cell_root;

    public DateSelectorCell(View itemView) {
        super(itemView);
        iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        tv_content = (TextView) itemView.findViewById(R.id.tv_content);
        tv_date = (TextView) itemView.findViewById(R.id.tv_date);
        ll_cell_root = (LinearLayout) itemView.findViewById(R.id.ll_cell_root);
    }

    @Override
    public Object value() {
        return tv_date.getText().toString();
    }
} public class DouyuHeadCell extends SLTableViewCell {
    public TextView cell_head_title_text;
    public LinearLayout cell_head_more_layout;
    public TextView cell_head_more_text;
    public DouyuHeadCell(View itemView) {
        super(itemView);
        cell_head_title_text = (TextView) itemView.findViewById(R.id.cell_head_title_text);
        cell_head_more_layout = (LinearLayout) itemView.findViewById(R.id.cell_head_more_layout);
        cell_head_more_text = (TextView) itemView.findViewById(R.id.cell_head_more_text);
    }
} public class DouyuHotAuthorCell extends SLTableViewCell {

    public TextView hot_author_name_text;
    public TextView hot_author_video_number_text;
    public TextView hot_author_subscribe_number_text;
    public LinearLayout room_layout;
    public DouyuHotAuthorCell(View itemView) {
        super(itemView);
        hot_author_name_text = (TextView) itemView.findViewById(R.id.hot_author_name_text);
        hot_author_video_number_text = (TextView) itemView.findViewById(R.id.hot_author_video_number_text);
        hot_author_subscribe_number_text = (TextView) itemView.findViewById(R.id.hot_author_subscribe_number_text);
        room_layout = (LinearLayout) itemView.findViewById(R.id.room_layout);
    }
} public class DouyuRoomCell extends SLTableViewCell {

    public TextView room_number_text;
    public TextView room_owner_text;
    public TextView room_title_text;
    public LinearLayout room_layout;
    public DouyuRoomCell(View itemView) {
        super(itemView);
        room_number_text = (TextView) itemView.findViewById(R.id.room_number_text);
        room_owner_text = (TextView) itemView.findViewById(R.id.room_owner_text);
        room_title_text = (TextView) itemView.findViewById(R.id.room_title_text);
        room_layout = (LinearLayout) itemView.findViewById(R.id.room_layout);
    }
} public class DouyuYanzhiCell extends SLTableViewCell {

    public TextView room_number_text;
    public TextView room_owner_text;
    public TextView room_location;
    public LinearLayout room_layout;
    public DouyuYanzhiCell(View itemView) {
        super(itemView);
        room_number_text = (TextView) itemView.findViewById(R.id.room_number_text);
        room_owner_text = (TextView) itemView.findViewById(R.id.room_owner_text);
        room_location = (TextView) itemView.findViewById(R.id.room_location);
        room_layout = (LinearLayout) itemView.findViewById(R.id.room_layout);
    }
} public class EditTextCell extends SLTableViewCell {

    public final ImageView iv_icon;
    public final EditText et_content;
    public EditTextCell(View itemView) {
        super(itemView);
        iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        et_content = (EditText) itemView.findViewById(R.id.et_content);
        et_content.setInputType(EditorInfo.TYPE_CLASS_TEXT);//默认 文字键盘
    }

    @Override
    public Object value() {
        return et_content.getText().toString();
    }

} public class EditTextUnitCell extends SLTableViewCell {

    public final ImageView iv_icon;
    public final EditText et_content;
    public final TextView tv_unit;

    public EditTextUnitCell(View itemView) {
        super(itemView);
        iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        et_content = (EditText) itemView.findViewById(R.id.et_content);
        tv_unit = (TextView) itemView.findViewById(R.id.tv_unit);
        et_content.setInputType(EditorInfo.TYPE_CLASS_TEXT);
    }

    @Override
    public Object value() {
        return et_content.getText().toString();
    }
} public class GenderSelectorCell extends SLTableViewCell {
    public final ImageView iv_icon;
    public final RadioButton rb_female;
    public final RadioButton rb_male;
    public GenderSelectorCell(View itemView) {
        super(itemView);
        iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        rb_female = (RadioButton) itemView.findViewById(R.id.rb_female);
        rb_male = (RadioButton) itemView.findViewById(R.id.rb_male);
    }

    @Override
    public Object value() {
        return rb_female.isChecked() ? "0":"1";
    }
} public class LineCell extends SLTableViewCell {
    public LineCell(View itemView) {
        super(itemView);
    }
} public class TextCell extends SLTableViewCell {

    public final ImageView iv_icon;
    public final TextView tv_content;
    public final LinearLayout ll_cell_root;
    public TextCell(View itemView) {
        super(itemView);
        iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
        tv_content = (TextView) itemView.findViewById(R.id.tv_content);
        ll_cell_root = (LinearLayout) itemView.findViewById(R.id.ll_cell_root);
    }
}
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;
import com.shanlin.library.sltableview.SLIndexPath;
import com.shanlin.library.sltableview.SLTableView;
import com.shanlin.library.sltableview.SLTableViewCell;
import com.shanlin.library.sltableview.SLTableViewDataSource;
import com.shanlin.library.sltableview.SLTableViewDelegate;
import com.shanlin.library.sltableview.SLTableViewLayoutManagerExpand;
import com.shanlin.sltableview.R;
import com.shanlin.sltableview.fragment.bean.ButtonBean;
import com.shanlin.sltableview.fragment.bean.CellBaseBean;
import com.shanlin.sltableview.fragment.bean.CellType;
import com.shanlin.sltableview.fragment.bean.CheckBoxBean;
import com.shanlin.sltableview.fragment.bean.DateSelectorBean;
import com.shanlin.sltableview.fragment.bean.DouyuHeadBean;
import com.shanlin.sltableview.fragment.bean.DouyuHotAuthorBean;
import com.shanlin.sltableview.fragment.bean.DouyuRoomBean;
import com.shanlin.sltableview.fragment.bean.DouyuYanzhiBean;
import com.shanlin.sltableview.fragment.bean.EditTextBean;
import com.shanlin.sltableview.fragment.bean.EditTextUnitBean;
import com.shanlin.sltableview.fragment.bean.GenderSelectorBean;
import com.shanlin.sltableview.fragment.bean.TextBean;
import com.shanlin.sltableview.fragment.cell.ButtonCell;
import com.shanlin.sltableview.fragment.cell.CheckBoxCell;
import com.shanlin.sltableview.fragment.cell.DateSelectorCell;
import com.shanlin.sltableview.fragment.cell.DouyuHeadCell;
import com.shanlin.sltableview.fragment.cell.DouyuHotAuthorCell;
import com.shanlin.sltableview.fragment.cell.DouyuRoomCell;
import com.shanlin.sltableview.fragment.cell.DouyuYanzhiCell;
import com.shanlin.sltableview.fragment.cell.EditTextCell;
import com.shanlin.sltableview.fragment.cell.EditTextUnitCell;
import com.shanlin.sltableview.fragment.cell.GenderSelectorCell;
import com.shanlin.sltableview.fragment.cell.LineCell;
import com.shanlin.sltableview.fragment.cell.TextCell;
import java.util.ArrayList;
import static com.shanlin.sltableview.fragment.bean.CellType.CELL_TYPE_DOUYU_HOT_AUTHOR;
package com.shanlin.sltableview.fragment.base;





/**
 *
 * 根据数据显示UI.
 * Created by Shanlin on 2017/1/1.
 */

public abstract  class CellAbstractFragment extends BaseFragment  implements SLTableViewDataSource,
        SLTableViewDelegate,
        SLTableViewLayoutManagerExpand,
        SLTableViewCell.SLCellViewClickListener
{

    protected   int table_padding ;
    protected   int table_padding_s2 ;


    protected ArrayList<ArrayList<CellBaseBean>> dataLists = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        table_padding = context.getResources().getDimensionPixelSize(R.dimen.cell_padding);
        table_padding_s2 = context.getResources().getDimensionPixelSize(R.dimen.cell_padding_s2);
    }

    //SLTableViewDataSource
    @Override
    public int numberOfSections(SLTableView tableView) {
        return dataLists.size();
    }

    @Override
    public int numberOfRowsInSection(SLTableView tableView, int section) {
        return dataLists.get(section).size();
    }

    @Override
    public int typeOfIndexPath(SLTableView tableView, SLIndexPath indexPath) {
        int section = indexPath.getSection();
        int row = indexPath.getRow();
        CellBaseBean baseBean =  dataLists.get(section).get(row);
        return baseBean.getType().getCellType();
    }

    @Override
    public SLTableViewCell cellForType(SLTableView tableView, ViewGroup parent, int type) {
        SLTableViewCell cell = null;
        View rootView;
        CellType cellType = CellType.typeOfCellType(type);
        switch (cellType){
            case CELL_TYPE_DOUYU_HEAD:
                rootView = inflater.inflate(R.layout.cell_douyu_head,parent,false);
                cell = new DouyuHeadCell(rootView);
                break;
            case CELL_TYPE_DOUYU_ROOM:
                rootView = inflater.inflate(R.layout.cell_douyu_room,parent,false);
                cell = new DouyuRoomCell(rootView);
                break;
            case CELL_TYPE_DOUYU_HOT_AUTHOR:
                rootView = inflater.inflate(R.layout.cell_douyu_hot_author,parent,false);
                cell = new DouyuHotAuthorCell(rootView);
                break;
            case CELL_TYPE_DOUYU_ROOM_YANZHI:
                rootView = inflater.inflate(R.layout.cell_douyu_yanzhi,parent,false);
                cell = new DouyuYanzhiCell(rootView);
                break;
            case CELL_TYPE_CHECK_BOX:
                rootView = inflater.inflate(R.layout.cell_check_box,parent,false);
                cell = new CheckBoxCell(rootView);
                break;
            case CELL_TYPE_DATE_SELECTOR:
                rootView = inflater.inflate(R.layout.cell_date_selector,parent,false);
                cell = new DateSelectorCell(rootView);
                break;
            case CELL_TYPE_EDIT_TEXT:
                rootView = inflater.inflate(R.layout.cell_edit_text,parent,false);
                cell = new EditTextCell(rootView);
                break;
            case CELL_TYPE_EDIT_TEXT_UNIT:
                rootView = inflater.inflate(R.layout.cell_edit_text_unit,parent,false);
                cell = new EditTextUnitCell(rootView);