Android:CourseTable Layout-使いやすいAndroid自動生成カリキュラムのカスタムコントロール

7439 ワード

転載は出典を明記してください.http://blog.csdn.net/hmyang314/article/details/52092446
CourseTableLayout
Android自動生成カリキュラムのカスタムコントロール
GitHub:https://github.com/HMY314/CourseTableLayout
Android:CourseTableLayout — 好用的Android自动生成课程表的自定义控件_第1张图片
一、紹介
    1、        ,     、    ,       ;
    2、               ,   、             ;
    3、   ,           ,   0,             ,           ;
    4、              ,      ,                ;

二、使用方法
1、コアクラスはCourseTableLayoutであり、LinearLayoutの抽象クラスから継承されているので、実際のプロジェクトの使用にはそれを継承する必要があります.ここでは汎用型を使用し、カリキュラムの使用をより柔軟にし、対応する抽象的な方法を実現します.以下のようにします.
    public abstract class CourseTableLayout extends LinearLayout {
    //******************************      **************************

            /**
             *                
             *
             * @param course
             * @param dayPosition
             * @param timePosition
             * @return   boolean      2,
             * boolean[0]:true               ,boolean[1]:true                
             */
            protected abstract boolean[] compareToCourse(T course, int dayPosition, int timePosition);

            /**
             *        TextView     
             *
             * @param firstTextView
             */
            protected void showFirstTextView(TextView firstTextView) {
            }

            /**
             *      (       ,         )
             */
            protected void showEmptyCourse(TextView textView, int dayPosition, int timePosition, int oneTableWidth, int oneTableHeight) {
            }

            /**
             *        ,                
             *
             * @param textView
             * @param course             
             * @param oneTableWidth          
             * @param oneTableHeight         
             */
            protected abstract void showCourse(TextView textView, T course, int dataPosition, int dayPosition, int timePosition, int oneTableWidth, int oneTableHeight);

            /**
             *        
             *
             * @param textView
             * @param dayPosition
             * @param dayLabel
             */
            protected void customDayText(TextView textView, int dayPosition, String dayLabel) {
            }

            /**
             *        
             *
             * @param textView
             * @param timePosition
             * @param timeLabel
             */
            protected void customTimeText(TextView textView, int timePosition, String timeLabel) {
            }

            protected abstract void onClickEmptyCourse(TextView textView, int dayPosition, int timePosition);

            protected abstract void onClickCourse(TextView textView, T course, int dataPosition, int dayPosition, int timePosition);
    }

2、私はここでcourseTableTestLayoutでCourseTableLayoutを継承し、データモデルはCourseModelエンティティを使用し、これは具体的な業務によって柔軟に変更することができる.
    public class CourseModel implements Serializable {
        private static final long serialVersionUID = 1L;

        public int week;
        public String name;
        /**
         *       
         */
        public int start;
        /**
         *     
         */
        public int step;
    }

    public class courseTableTestLayout extends CourseTableLayout {

        private int BG_COURSE[] = new int[]{R.drawable.bg_course_table_blue_selector, R.drawable.bg_course_table_green_selector,
                R.drawable.bg_course_table_red_selector};

        private OnClickCourseListener mOnClickCourseListener;

        public courseTableTestLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            initData();
        }

        public courseTableTestLayout(Context context) {
            super(context);
            initData();
        }

        private void initData() {
            setIsShowDefault(true);
        }

        @Override
        protected boolean[] compareToCourse(CourseModel course, int dayPosition, int timePosition) {
            boolean[] result = new boolean[2];
            if (course.week == dayPosition + 1 && course.start == timePosition + 1) {
                result[0] = true;
            }
            return result;
        }

        @Override
        protected void showCourse(TextView textView, CourseModel course, int dataPosition, int dayPosition, int timePosition, int oneTableWidth, int oneTableHeight) {
            textView.setBackgroundResource(R.drawable.bg_course_table_blue_selector);
        }

        @Override
        protected void customDayText(TextView textView, int dayPosition, String dayLabel) {

        }

        @Override
        protected void onClickEmptyCourse(TextView textView, int dayPosition, int timePosition) {
            if (mOnClickCourseListener != null) {
                mOnClickCourseListener.onClickEmptyCourse(textView, dayPosition, timePosition);
            }
        }

        @Override
        protected void onClickCourse(TextView textView, CourseModel course, int dataPosition, int dayPosition, int timePosition) {
            if (mOnClickCourseListener != null) {
                mOnClickCourseListener.onClickCourse(textView, course, dataPosition, dayPosition, timePosition);
            }
        }

        public void setOnClickCourseListener(OnClickCourseListener listener) {
            mOnClickCourseListener = listener;
        }

        public interface OnClickCourseListener {
            void onClickCourse(TextView textView, CourseModel course, int dataPosition, int dayPosition, int timePosition);

            void onClickEmptyCourse(TextView textView, int dayPosition, int timePosition);
        }
    }

3、xmlで実現
        
        

4、使用:
    List mList = getData();//    
    CourseTableTestLayout mCourseTableTestLayout = (CourseTableTestLayout) findViewById(R.id.layout_course);
    //             
    mCourseTableTestLayout.setData(mList);

    //        
    mCourseTableTestLayout.notifyDataSetChanged();

Android:CourseTableLayout — 好用的Android自动生成课程表的自定义控件_第2张图片 Android:CourseTableLayout — 好用的Android自动生成课程表的自定义控件_第3张图片