前言
最近在學(xué)習(xí)課程表的制作方法,并沒有發(fā)現(xiàn)一些已經(jīng)封裝好的課表開源庫,于是突發(fā)奇想封裝了一個(gè)這樣的課程表控件,正好也學(xué)習(xí)了如果發(fā)布自己的第三方庫。
項(xiàng)目的地址:CourseTableView
推薦閱讀:教你自己寫Android第三方庫
介紹
CourseTableView 是一個(gè)讓你快速方便的創(chuàng)建課程表視圖的庫

CourseTableView.png
平臺(tái)要求
- Android SDK 15+
基本使用
- 在你的配置文件中添加 JitPack的代碼倉庫
- Gradle
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Maven
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
- 添加依賴
- Gradle
dependencies {
compile 'com.github.YueYongDev:CourseTableView:v1.0'
}
- Maven
<dependency>
<groupId>com.github.YueYongDev</groupId>
<artifactId>CourseTableView</artifactId>
<version>v1.0</version>
</dependency>
- 在你想要展示你的課程表的那個(gè)界面添加“CourseTableView”
<cn.lyy.coursetable.CourseTableView
android:id="@+id/ctv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- 同時(shí),你需要在Activity中創(chuàng)建
Course對(duì)象
Course c1 = new Course();
c1.setDay(1);
c1.setDes("第一節(jié)課");
c1.setJieci(1);
c1.setBg_Color(color[2]);
Course c2 = new Course();
c2.setDay(6);
c2.setDes("第一節(jié)課");
c2.setJieci(3);
c2.setBg_Color(color[3]);
- 創(chuàng)建一個(gè)集合,并把之前創(chuàng)建的對(duì)象添加進(jìn)去
List<Course> list = new ArrayList<>();
list.add(c1);
list.add(c2);
- 初始化你的
coursetableview并且執(zhí)行updateCourseViews方法
CourseTableView courseTableView = (CourseTableView) findViewById(R.id.ctv);
courseTableView.updateCourseViews(list);
擴(kuò)展
更改課程背景
你可以使用以下的預(yù)置背景:
private int color[] = {
R.drawable.course_info_blue,
R.drawable.course_info_brown,
R.drawable.course_info_cyan,
R.drawable.course_info_deep_orange,
R.drawable.course_info_deep_purple,
R.drawable.course_info_green,
R.drawable.course_info_indigo,
R.drawable.course_info_light_blue,
R.drawable.course_info_light_green,
R.drawable.course_info_lime,
R.drawable.course_info_orange,
R.drawable.course_info_pink,
R.drawable.course_info_purple,
R.drawable.course_info_red,
R.drawable.course_info_teal,
R.drawable.course_info_yellow
};
也可以選擇自定義你的背景:
- 創(chuàng)建一個(gè) Drawable resource file.
bg_resource.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#A5D6A7" />
<corners android:radius="5dp" />
</shape>
- 使用這個(gè)資源文件作為背景
int bg_resource = R.drawable.bg_resource;
c3.setBg_Color(bg_resource);
使課表可以點(diǎn)擊
執(zhí)行以下方法便可以響應(yīng)點(diǎn)擊事件
courseTableView.setOnCourseItemClickListener(new CourseTableView.OnCourseItemClickListener() {
@Override
public void onCourseItemClick(TextView tv, int jieci, int day, String des) {
String string = tv.getText().toString();
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
}
});
用法例子詳見Demo