由于毕业设计有一个功能模块是课程表,就想模仿一下超级课程表的界面,可是开始做的时候却没有一点头绪,百度google均无果,在CSDN和知乎上提问了也没人回答(估计是太简单了 大神不愿回答
),总之自己鼓捣了几天还是弄出来了,虽然实现的方法很挫。。。因为有好几个人都发私信问我怎么实现的,现在毕设做完了,所以我干脆就写到博客上吧,先上几张效果图:
当时看到超级课程表的界面时,第一个想法就是使用ListView来实现,好不容易把格子画出来了,课程信息不知道怎么放上去····,主要难点有:
1、
第一排的8个格子是固定的,下面的课表信息部分是可以滑动的,单用ListView无法实现,即下图。
2、
课程信息怎么附着在格子上,并且可以随着课表一起滚动。
放弃了ListView实现的想法,就只有另寻它路了,在CSDN上有一位朋友的回答给了我灵感,即每一个格子都用一个TextView实现。然后课程信息可以使用相对布局,根据课程的时间(节数和天数)算出他的偏移位置,比如星期二的第三、四节课就可以和周二下面的第一个格子保持左对齐和上对齐,并且向下偏移2个格子的距离。这个N个格子和课程信息都放到一个RelativeLayout中,然后再在外面嵌套一个ScrollViewLayout,就可以滚动了,总的布局还是RelayoutLayout,不多说了,直接上代码!
-
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
-
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
-
android:layout_width
=
"fill_parent"
-
android:layout_height
=
"fill_parent"
-
android:background
=
"@drawable/c_bg"
>
-
-
-
<
TextView
android:id
=
"@+id/test_empty"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
style
=
"@style/courseTableText"
-
android:text
=
"@string/empty"
-
android:background
=
"@drawable/course_text_view_bg"
-
/>
-
-
-
<
TextView
android:id
=
"@+id/test_monday_course"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"@string/mon"
-
style
=
"@style/courseTableText"
-
android:layout_toRightOf
=
"@id/test_empty"
-
android:background
=
"@drawable/course_text_view_bg"
-
/>
-
-
-
-
<
TextView
android:id
=
"@+id/test_tuesday_course"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"@string/tue"
-
style
=
"@style/courseTableText"
-
android:layout_toRightOf
=
"@id/test_monday_course"
-
android:background
=
"@drawable/course_text_view_bg"
-
/>
-
-
-
<
TextView
android:id
=
"@+id/test_wednesday_course"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"@string/wen"
-
style
=
"@style/courseTableText"
-
android:layout_toRightOf
=
"@id/test_tuesday_course"
-
android:background
=
"@drawable/course_text_view_bg"
-
/>
-
-
-
<
TextView
android:id
=
"@+id/test_thursday_course"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"@string/thu"
-
style
=
"@style/courseTableText"
-
android:layout_toRightOf
=
"@id/test_wednesday_course"
-
android:background
=
"@drawable/course_text_view_bg"
-
/>
-
-
<
TextView
android:id
=
"@+id/test_friday_course"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"@string/fri"
-
style
=
"@style/courseTableText"
-
android:layout_toRightOf
=
"@id/test_thursday_course"
-
android:background
=
"@drawable/course_text_view_bg"
-
/>
-
-
-
<
TextView
android:id
=
"@+id/test_saturday_course"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:text
=
"@string/sta"
-
style
=
"@style/courseTableText"
-
android:layout_toRightOf
=
"@id/test_friday_course"
-
android:background
=
"@drawable/course_text_view_bg"
-
/>
-
-
-
<
TextView
android:id
=
"@+id/test_sunday_course"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
style
=
"@style/courseTableText"
-
android:text
=
"@string/sun"
-
android:layout_toRightOf
=
"@id/test_saturday_course"
-
android:background
=
"@drawable/course_table_last_colum"
-
/>
-
-
-
<
ScrollView
-
android:id
=
"@+id/scroll_body"
-
android:layout_width
=
"fill_parent"
-
android:layout_height
=
"wrap_content"
-
android:layout_below
=
"@id/test_empty"
-
android:scrollbars
=
"none"
-
>
-
-
<
RelativeLayout
-
android:layout_width
=
"fill_parent"
-
android:layout_height
=
"wrap_content"
-
android:id
=
"@+id/test_course_rl"
-
>
-
</
RelativeLayout
>
-
-
</
ScrollView
>
-
</
RelativeLayout
>
2、用TextView表示一个格子,即有边框的TextView,整个课表是由N个TextView组成的,所以就没办法使用一张图当背景了。
当然现在我也想到了改进的方法,其实给课程信息做参照算出偏移位置的只需要一个格子就可以了,所以可以用一个透明的TextView放在星期一的第一个格子的位置作为参照,格子使用view画线实现,就可以使用背景了,也不用生成多个TextView了···这是我的想法,我还没用代码实现,有兴趣的朋友可以自己去试试。
超级课程表还有一个要点就是它的课程展示3D视图,不过那个可以用
Android
自带的gallery实现,网上有现成的代码,拿过来改改就可以了,下面上主要代码
1、CourseTableActivity
-
package
nd.leiyi.crims.activity;
-
-
import
java.lang.ref.WeakReference;
-
import
java.util.ArrayList;
-
import
java.util.Collections;
-
import
java.util.Comparator;
-
import
java.util.HashMap;
-
import
java.util.Iterator;
-
import
java.util.List;
-
import
java.util.Map;
-
-
import
nd.leiyi.crims.R;
-
import
nd.leiyi.crims.adapter.CourseInfoAdapter;
-
import
nd.leiyi.crims.appException.AppException;
-
import
nd.leiyi.crims.constant.UserInfo;
-
import
nd.leiyi.crims.db.CourseInfoDBManager;
-
import
nd.leiyi.crims.gallery3D.CourseInfoGallery;
-
import
nd.leiyi.crims.http.CourseInfoFetcher;
-
import
nd.leiyi.crims.model.CourseInfo;
-
import
nd.leiyi.crims.util.CourseSettingUtil;
-
import
android.app.Activity;
-
import
android.app.AlertDialog;
-
import
android.app.Dialog;
-
import
android.app.ProgressDialog;
-
import
android.content.Context;
-
import
android.content.Intent;
-
import
android.content.SharedPreferences;
-
import
android.content.SharedPreferences.Editor;
-
import
android.graphics.Color;
-
import
android.graphics.drawable.BitmapDrawable;
-
import
android.graphics.drawable.Drawable;
-
import
android.os.Bundle;
-
import
android.os.Handler;
-
import
android.os.Message;
-
import
android.util.DisplayMetrics;
-
import
android.util.Log;
-
import
android.view.Gravity;
-
import
android.view.LayoutInflater;
-
import
android.view.View;
-
import
android.view.View.OnClickListener;
-
import
android.view.ViewGroup.LayoutParams;
-
import
android.view.Window;
-
import
android.view.WindowManager;
-
import
android.widget.AdapterView;
-
import
android.widget.AdapterView.OnItemClickListener;
-
import
android.widget.ListView;
-
import
android.widget.PopupWindow;
-
import
android.widget.PopupWindow.OnDismissListener;
-
import
android.widget.Button;
-
import
android.widget.RelativeLayout;
-
import
android.widget.SimpleAdapter;
-
import
android.widget.TextView;
-
import
android.widget.Toast;
-
-
public
class
CourseTableActivity
extends
Activity {
-
-
-
protected
TextView textTitle;
-
-
protected
TextView empty;
-
-
protected
TextView monColum;
-
-
protected
TextView tueColum;
-
-
protected
TextView wedColum;
-
-
protected
TextView thrusColum;
-
-
protected
TextView friColum;
-
-
protected
TextView satColum;
-
-
protected
TextView sunColum;
-
-
protected
RelativeLayout course_table_layout;
-
-
protected
PopupWindow weekListWindow;
-
-
protected
ListView weekListView;
-
-
protected
View popupWindowLayout;
-
-
protected
Map<String, List<CourseInfo>> courseInfoMap;
-
-
protected
List<TextView> courseTextViewList =
new
ArrayList<TextView>();
-
-
protected
Map<Integer, List<CourseInfo>> textviewCourseInfoMap =
new
HashMap<Integer, List<CourseInfo>>();
-
-
protected
int
aveWidth;
-
-
protected
int
screenWidth;
-
-
protected
int
gridHeight =
80
;
-
-
protected
int
maxCourseNum;
-
-
protected
Button goBackButton;
-
-
protected
ProgressDialog pDialog;
-
-
protected
Handler mhandler =
new
Handler();
-
@Override
-
protected
void
onCreate(Bundle savedInstanceState) {
-
-
super
.onCreate(savedInstanceState);
-
-
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
-
setContentView(R.layout.course_table_layout);
-
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);
-
-
textTitle = (TextView)
this
.findViewById(R.id.textTile);
-
textTitle.setTextSize(
20
);
-
textTitle.setPadding(
15
,
2
,
15
,
2
);
-
-
Drawable down =
this
.getResources().getDrawable(R.drawable.title_down);
-
down.setBounds(
0
,
0
, down.getMinimumWidth(), down.getMinimumHeight());
-
textTitle.setCompoundDrawables(
null
,
null
, down,
null
);
-
textTitle.setCompoundDrawablePadding(
2
);
-
-
empty = (TextView)
this
.findViewById(R.id.test_empty);
-
monColum = (TextView)
this
.findViewById(R.id.test_monday_course);
-
tueColum = (TextView)
this
.findViewById(R.id.test_tuesday_course);
-
wedColum = (TextView)
this
.findViewById(R.id.test_wednesday_course);
-
thrusColum = (TextView)
this
.findViewById(R.id.test_thursday_course);
-
friColum = (TextView)
this
.findViewById(R.id.test_friday_course);
-
satColum = (TextView)
this
.findViewById(R.id.test_saturday_course);
-
sunColum = (TextView)
this
.findViewById(R.id.test_sunday_course);
-
-
goBackButton = (Button)
this
.findViewById(R.id.button_go_back);
-
goBackButton.setOnClickListener(
new
OnClickListener() {
-
@Override
-
public
void
onClick(View arg0) {
-
-
goBackButton.setBackgroundDrawable(CourseTableActivity.
this
-
.getResources().getDrawable(R.drawable.arrow_left_down));
-
-
mhandler.postDelayed(
new
Runnable() {
-
@Override
-
public
void
run() {
-
goBackButton.setBackgroundDrawable(CourseTableActivity.
this
-
.getResources().getDrawable(R.drawable.arrow_left));
-
}
-
},
200
);
-
mhandler.postDelayed(
new
Runnable() {
-
@Override
-
public
void
run() {
-
finish();
-
}
-
},
400
);
-
-
}
-
});
-
-
course_table_layout = (RelativeLayout)
this
.findViewById(R.id.test_course_rl);
-
DisplayMetrics dm =
new
DisplayMetrics();
-
getWindowManager().getDefaultDisplay().getMetrics(dm);
-
-
int
width = dm.widthPixels;
-
-
-
int
aveWidth = width /
8
;
-
-
empty.setWidth(aveWidth *
3
/
4
);
-
monColum.setWidth(aveWidth *
33
/
32
+
1
);
-
tueColum.setWidth(aveWidth *
33
/
32
+
1
);
-
wedColum.setWidth(aveWidth *
33
/
32
+
1
);
-
thrusColum.setWidth(aveWidth *
33
/
32
+
1
);
-
friColum.setWidth(aveWidth *
33
/
32
+
1
);
-
satColum.setWidth(aveWidth *
33
/
32
+
1
);
-
sunColum.setWidth(aveWidth *
33
/
32
+
1
);
-
this
.screenWidth = width;
-
this
.aveWidth = aveWidth;
-
-
init();
-
}
-
-
-
-
-
protected
void
init(){
-
-
-
-
final
SharedPreferences courseSettings = getSharedPreferences(
"course_setting"
, Activity.MODE_PRIVATE);
-
-
if
(courseSettings.getString(
"currentTerm_"
+ UserInfo.currentUser.getStuNum(),
null
) ==
null
)
-
{
-
Toast.makeText(CourseTableActivity.
this
,
"您尚未设置当前学期!快去设置吧!"
, Toast.LENGTH_SHORT).show();
-
return
;
-
}
-
-
final
String currentWeekStr = CourseSettingUtil.figureCurrentWeek(courseSettings);
-
if
(currentWeekStr.equals(
""
))
-
{
-
textTitle.setText(
"全部"
);
-
}
-
else
-
{
-
textTitle.setText(
"第"
+ currentWeekStr +
"周"
);
-
-
}
-
-
textTitle.setOnClickListener(
new
OnClickListener() {
-
@Override
-
public
void
onClick(View arg0) {
-
-
-
textTitle.setBackgroundDrawable(
-
CourseTableActivity.
this
.getResources().getDrawable(R.drawable.title_text_bg));
-
-
showWeekListWindow(textTitle);
-
}
-
});
-
-
String maxCourseNumStr = courseSettings.getString(
"maxCourseNum_"
+ UserInfo.currentUser.getStuNum(),
""
);
-
if
(maxCourseNumStr.equals(
""
))
-
{
-
courseSettings.edit().putString(
"maxCourseNum_"
+ UserInfo.currentUser.getStuNum(),
"12"
);
-
maxCourseNum =
12
;
-
}
-
else
-
{
-
maxCourseNum = Integer.parseInt(maxCourseNumStr);
-
}
-
DisplayMetrics dm =
new
DisplayMetrics();
-
getWindowManager().getDefaultDisplay().getMetrics(dm);
-
-
int
height = dm.heightPixels;
-
gridHeight = height / maxCourseNum;
-
-
-
for
(
int
i =
1
; i <= maxCourseNum; i ++){
-
-
for
(
int
j =
1
; j <=
8
; j ++){
-
-
TextView tx =
new
TextView(CourseTableActivity.
this
);
-
tx.setId((i -
1
) *
8
+ j);
-
-
if
(j <
8
)
-
tx.setBackgroundDrawable(CourseTableActivity.
this
.
-
getResources().getDrawable(R.drawable.course_text_view_bg));
-
else
-
tx.setBackgroundDrawable(CourseTableActivity.
this
.
-
getResources().getDrawable(R.drawable.course_table_last_colum));
-
-
RelativeLayout.LayoutParams rp =
new
RelativeLayout.LayoutParams(
-
aveWidth *
33
/
32
+
1
,
-
gridHeight);
-
-
tx.setGravity(Gravity.CENTER);
-
-
tx.setTextAppearance(
this
, R.style.courseTableText);
-
-
if
(j ==
1
)
-
{
-
tx.setText(String.valueOf(i));
-
rp.width = aveWidth *
3
/
4
;
-
-
if
(i ==
1
)
-
rp.addRule(RelativeLayout.BELOW, empty.getId());
-
else
-
rp.addRule(RelativeLayout.BELOW, (i -
1
) *
8
);
-
}
-
else
-
{
-
rp.addRule(RelativeLayout.RIGHT_OF, (i -
1
) *
8
+ j -
1
);
-
rp.addRule(RelativeLayout.ALIGN_TOP, (i -
1
) *
8
+ j -
1
);
-
tx.setText(
""
);
-
}
-
-
tx.setLayoutParams(rp);
-
course_table_layout.addView(tx);
-
}
-
}
-
-
-
-
pDialog =
new
ProgressDialog(
this
);
-
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
-
pDialog.setMessage(
"正在加载信息。。。。"
);
-
pDialog.setIndeterminate(
true
);
-
pDialog.setCanceledOnTouchOutside(
false
);
-
pDialog.setCancelable(
false
);
-
pDialog.show();
-
-
-
-
final
String currentTerm = courseSettings.getString(
"currentTerm_"
+ UserInfo.currentUser.getStuNum(),
null
);
-
-
final
boolean
hasSetting = courseSettings.getBoolean(
"Is_"
+ currentTerm +
"_saved_"
+ UserInfo.currentUser.getStuNum(),
false
);
-
-
-
-
new
Thread() {
-
@Override
-
public
void
run() {
-
-
CourseInfoDBManager dbManager =
new
CourseInfoDBManager(CourseTableActivity.
this
);
-
-
dbManager.open();
-
-
try
{
-
-
if
(!hasSetting)
-
{
-
-
List<CourseInfo> list = CourseInfoFetcher.fetchCourseInfo(
""
, currentTerm, UserInfo.currentUser.getStuNum());
-
-
for
(CourseInfo courseInfo : list)
-
{
-
dbManager.insertCourse(courseInfo, currentTerm);
-
}
-
-
Editor editor = courseSettings.edit();
-
editor.putBoolean(
"Is_"
+ currentTerm +
"_saved_"
+ UserInfo.currentUser.getStuNum(),
true
);
-
editor.commit();
-
-
}
-
-
-
courseInfoMap = dbManager.query(currentTerm);
-
-
Message msg =
new
Message();
-
if
(courseInfoMap.isEmpty())
-
{
-
msg.what = -
2
;
-
courseInfoInitMessageHandler.sendMessage(msg);
-
return
;
-
}
-
int
currentWeek = -
1
;
-
if
(!currentWeekStr.equals(
""
))
-
{
-
currentWeek = Integer.parseInt(currentWeekStr);
-
}
-
dbManager.close();
-
InitMessageObj msgObj =
new
InitMessageObj(aveWidth, currentWeek, screenWidth, maxCourseNum);
-
msg.obj = msgObj;
-
courseInfoInitMessageHandler.sendMessage(msg);
-
-
}
catch
(AppException e) {
-
Message msg =
new
Message();
-
msg.what = -
1
;
-
courseInfoInitMessageHandler.sendMessage(msg);
-
Log.e(
"courseInfo_fetch_exception"
, e.toString());
-
-
}
finally
{
-
-
dbManager.close();
-
}
-
}
-
}.start();
-
-
}
-
-
CourseInfoInitMessageHandler courseInfoInitMessageHandler =
new
CourseInfoInitMessageHandler(
this
);
-
-
static
class
InitMessageObj{
-
-
int
aveWidth;
-
int
currentWeek;
-
int
screenWidth;
-
int
maxCourseNum;
-
public
InitMessageObj(
int
aveWidth,
int
currentWeek,
int
screenWidth,
int
maxCourseNum) {
-
super
();
-
this
.aveWidth = aveWidth;
-
this
.currentWeek = currentWeek;
-
this
.screenWidth = screenWidth;
-
this
.maxCourseNum = maxCourseNum;
-
}
-
-
}
-
-
static
class
CourseInfoInitMessageHandler
extends
Handler{
-
-
WeakReference<CourseTableActivity> mActivity;
-
-
public
CourseInfoInitMessageHandler(CourseTableActivity activity){
-
-
mActivity =
new
WeakReference<CourseTableActivity>(activity);
-
}
-
-
@Override
-
public
void
handleMessage(Message msg) {
-
-
mActivity.get().pDialog.dismiss();
-
-
if
(msg.what == -
1
)
-
{
-
Toast.makeText(mActivity.get(),
"获取课程信息失败!请检查您的网络或者稍后再试"
, Toast.LENGTH_SHORT).show();
-
return
;
-
}
-
-
if
(msg.what == -
2
)
-
{
-
Toast.makeText(mActivity.get(),
"教务管理系统中无该学期的课程信息···"
, Toast.LENGTH_SHORT).show();
-
return
;
-
}
-
-
int
[] background = {R.drawable.course_info_blue, R.drawable.course_info_green,
-
R.drawable.course_info_red, R.drawable.course_info_red,
-
R.drawable.course_info_yellow};
-
-
Map<String, List<CourseInfo>> courseInfoMap = mActivity.get().courseInfoMap;
-
-
final
InitMessageObj msgObj = (InitMessageObj) msg.obj;
-
-
int
currentWeek = msgObj.currentWeek;
-
-
int
maxCourseNum = msgObj.maxCourseNum;
-
for
(Map.Entry<String, List<CourseInfo>> entry: courseInfoMap.entrySet())
-
{
-
-
-
CourseInfo upperCourse =
null
;
-
-
final
List<CourseInfo> list =
new
ArrayList<CourseInfo>(entry.getValue());
-
-
-
Collections.sort(list,
new
Comparator<CourseInfo>(){
-
@Override
-
public
int
compare(CourseInfo arg0, CourseInfo arg1) {
-
-
if
(arg0.getBeginIndex() < arg1.getBeginIndex())
-
return
-
1
;
-
else
-
return
1
;
-
}
-
-
});
-
int
lastListSize;
-
do
{
-
-
lastListSize = list.size();
-
Iterator<CourseInfo> iter = list.iterator();
-
-
while
(iter.hasNext())
-
{
-
CourseInfo c = iter.next();
-
if
(((c.getBeginWeek() <= currentWeek && c.getEndWeek() >= currentWeek) || currentWeek == -
1
) && c.getEndIndex() <= maxCourseNum)
-
{
-
-
if
(c.getCourseType() == CourseInfo.ALL ||
-
(c.getCourseType() == CourseInfo.EVEN && currentWeek %
2
==
0
) ||
-
(c.getCourseType() == CourseInfo.ODD && currentWeek %
2
!=
0
) )
-
{
-
-
iter.remove();
-
upperCourse = c;
-
break
;
-
}
-
}
-
}
-
if
(upperCourse !=
null
)
-
{
-
List<CourseInfo> courseInfoList =
new
ArrayList<CourseInfo>();
-
courseInfoList.add(upperCourse);
-
int
index =
0
;
-
iter = list.iterator();
-
-
while
(iter.hasNext())
-
{
-
CourseInfo c = iter.next();
-
-
if
((c.getBeginIndex() <= upperCourse.getBeginIndex()
-
&&upperCourse.getBeginIndex() < c.getEndIndex())
-
||(upperCourse.getBeginIndex() <= c.getBeginIndex()
-
&& c.getBeginIndex() < upperCourse.getEndIndex()))
-
{
-
courseInfoList.add(c);
-
iter.remove();
-
-
if
((c.getEndIndex() - c.getEndIndex()) > (upperCourse.getEndIndex() - upperCourse.getBeginIndex())
-
&& ((c.getBeginWeek() <= currentWeek && c.getEndWeek() >= currentWeek) || currentWeek == -
1
))
-
{
-
upperCourse = c;
-
index ++;
-
}
-
-
}
-
-
}
-
-
final
int
upperCourseIndex = index;
-
-
TextView courseInfo =
new
TextView(mActivity.get());
-
courseInfo.setId(
1000
+ upperCourse.getDay() *
100
+ upperCourse.getBeginIndex() *
10
+ upperCourse.getId());
-
int
id = courseInfo.getId();
-
mActivity.get().textviewCourseInfoMap.put(id, courseInfoList);
-
courseInfo.setText(upperCourse.getCourseName() +
"\n@"
+ upperCourse.getClassRoom());
-
-
RelativeLayout.LayoutParams rlp =
new
RelativeLayout.LayoutParams(
-
msgObj.aveWidth *
31
/
32
,
-
(mActivity.get().gridHeight -
5
) *
2
+ (upperCourse.getEndIndex() - upperCourse.getBeginIndex() -
1
) * mActivity.get().gridHeight);
-
-
rlp.topMargin =
5
+ (upperCourse.getBeginIndex() -
1
) * mActivity.get().gridHeight;
-
rlp.leftMargin =
1
;
-
-
rlp.addRule(RelativeLayout.RIGHT_OF, upperCourse.getDay());
-
-
courseInfo.setGravity(Gravity.CENTER);
-
-
int
colorIndex = ((upperCourse.getBeginIndex() -
1
) *
8
+ upperCourse.getDay()) % (background.length -
1
);
-
courseInfo.setBackgroundResource(background[colorIndex]);
-
courseInfo.setTextSize(
12
);
-
courseInfo.setLayoutParams(rlp);
-
courseInfo.setTextColor(Color.WHITE);
-
-
courseInfo.getBackground().setAlpha(
222
);
-
-
courseInfo.setOnClickListener(
new
OnClickListener() {
-
@Override
-
public
void
onClick(View arg0) {
-
Log.i(
"text_view"
, String.valueOf(arg0.getId()));
-
Map<Integer, List<CourseInfo>> map = mActivity.get().textviewCourseInfoMap;
-
final
List<CourseInfo> tempList = map.get(arg0.getId());
-
if
(tempList.size() >
1
)
-
{
-
-
LayoutInflater layoutInflater = (LayoutInflater) mActivity.get().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
View galleryView = layoutInflater.inflate(R.layout.course_info_gallery_layout,
null
);
-
final
Dialog coursePopupDialog =
new
AlertDialog.Builder(mActivity.get()).create();
-
coursePopupDialog.setCanceledOnTouchOutside(
true
);
-
coursePopupDialog.setCancelable(
true
);
-
coursePopupDialog.show();
-
WindowManager.LayoutParams params = coursePopupDialog.getWindow().getAttributes();
-
params.width = LayoutParams.FILL_PARENT;
-
coursePopupDialog.getWindow().setAttributes(params);
-
CourseInfoAdapter adapter =
new
CourseInfoAdapter(mActivity.get(), tempList, msgObj.screenWidth, msgObj.currentWeek);
-
CourseInfoGallery gallery = (CourseInfoGallery) galleryView.findViewById(R.id.course_info_gallery);
-
gallery.setSpacing(
10
);
-
gallery.setAdapter(adapter);
-
gallery.setSelection(upperCourseIndex);
-
gallery.setOnItemClickListener(
new
OnItemClickListener() {
-
@Override
-
public
void
onItemClick(
-
AdapterView<?> arg0, View arg1,
-
int
arg2,
long
arg3) {
-
CourseInfo courseInfo = tempList.get(arg2);
-
Intent intent =
new
Intent();
-
Bundle mBundle =
new
Bundle();
-
mBundle.putSerializable(
"courseInfo"
, courseInfo);
-
intent.putExtras(mBundle);
-
intent.setClass(mActivity.get(), DetailCourseInfoActivity.
class
);
-
mActivity.get().startActivity(intent);
-
coursePopupDialog.dismiss();
-
}
-
});
-
coursePopupDialog.setContentView(galleryView);
-
}
-
else
-
{
-
Intent intent =
new
Intent();
-
Bundle mBundle =
new
Bundle();
-
mBundle.putSerializable(
"courseInfo"
, tempList.get(
0
));
-
intent.putExtras(mBundle);
-
intent.setClass(mActivity.get(), DetailCourseInfoActivity.
class
);
-
mActivity.get().startActivity(intent);
-
}
-
}
-
-
});
-
mActivity.get().course_table_layout.addView(courseInfo);
-
mActivity.get().courseTextViewList.add(courseInfo);
-
upperCourse =
null
;
-
}
-
}
while
(list.size() < lastListSize && list.size() !=
0
);
-
}
-
super
.handleMessage(msg);
-
}
-
-
}
-
-
-
-
-
private
void
showWeekListWindow(View parent){
-
-
if
(weekListWindow ==
null
)
-
{
-
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
-
popupWindowLayout = layoutInflater.inflate(R.layout.week_list_layout,
null
);
-
weekListView = (ListView) popupWindowLayout.findViewById(R.id.week_list_view_body);
-
-
weekListView.setVerticalScrollBarEnabled(
false
);
-
List<Map<String, Object>> weekList =
new
ArrayList<Map<String, Object>>();
-
-
for
(
int
i =
1
; i <=
25
; i ++)
-
{
-
Map<String, Object> rowData =
new
HashMap<String, Object>();
-
rowData.put(
"week_index"
,
"第"
+ i +
"周"
);
-
weekList.add(rowData);
-
}
-
-
-
SimpleAdapter listAdapter =
new
SimpleAdapter(
this
,
-
weekList, R.layout.week_list_item_layout,
-
new
String[]{
"week_index"
},
-
new
int
[]{R.id.week_list_item});
-
weekListView.setAdapter(listAdapter);
-
weekListView.setOnItemClickListener(
new
OnItemClickListener() {
-
@Override
-
public
void
onItemClick(AdapterView<?> adpater, View arg1,
-
int
arg2,
long
arg3) {
-
int
index =
0
;
-
String indexStr = textTitle.getText().toString();
-
indexStr = indexStr.replaceAll(
"第"
,
""
).replaceAll(
"周"
,
""
);
-
if
(!indexStr.equals(
"全部"
))
-
index = Integer.parseInt(indexStr);
-
textTitle.setText(
"第"
+ (arg2 +
1
) +
"周"
);
-
weekListWindow.dismiss();
-
if
((arg2 +
1
) != index)
-
{
-
Log.i(
"courseTableActivity"
,
"清空当前课程信息"
);
-
for
(TextView tx : courseTextViewList)
-
{
-
course_table_layout.removeView(tx);
-
}
-
courseTextViewList.clear();
-
-
Message msg =
new
Message();
-
InitMessageObj msgObj =
new
InitMessageObj(aveWidth, arg2 +
1
, screenWidth, maxCourseNum);
-
msg.obj = msgObj;
-
courseInfoInitMessageHandler.sendMessage(msg);
-
}
-
}
-
});
-
int
width = textTitle.getWidth();
-
-
weekListWindow =
new
PopupWindow(popupWindowLayout, width +
100
, width +
120
);
-
-
}
-
-
weekListWindow.setFocusable(
true
);
-
-
weekListWindow.setOutsideTouchable(
true
);
-
weekListWindow.setBackgroundDrawable(
new
BitmapDrawable());
-
-
weekListWindow.setOnDismissListener(
new
OnDismissListener() {
-
@Override
-
public
void
onDismiss() {
-
textTitle.setBackgroundDrawable(
null
);
-
}
-
});
-
weekListWindow.showAsDropDown(parent, -
50
,
0
);
-
}
-
-
}
2、CourseInfo
-
package
nd.leiyi.crims.model;
-
-
import
java.io.Serializable;
-
-
-
public
class
CourseInfo
implements
Serializable{
-
-
-
-
-
private
static
final
long
serialVersionUID = 2074656067805712769L;
-
-
private
int
id;
-
-
private
String courseName;
-
-
private
String classRoom;
-
-
private
String teacher;
-
-
private
int
day;
-
-
private
int
beginIndex;
-
-
private
int
endIndex;
-
-
private
int
beginWeek;
-
-
private
int
endWeek;
-
-
private
int
courseType;
-
-
public
static
final
int
ALL =
1
;
-
public
static
final
int
ODD =
2
;
-
public
static
final
int
EVEN =
3
;
-
-
public
String getCourseName() {
-
return
courseName;
-
}
-
public
void
setCourseName(String courseName) {
-
this
.courseName = courseName;
-
}
-
public
String getClassRoom() {
-
return
classRoom;
-
}
-
public
void
setClassRoom(String classRoom) {
-
this
.classRoom = classRoom;
-
}
-
public
String getTeacher() {
-
return
teacher;
-
}
-
public
void
setTeacher(String teacher) {
-
this
.teacher = teacher;
-
}
-
public
int
getDay() {
-
return
day;
-
}
-
public
void
setDay(
int
day) {
-
this
.day = day;
-
}
-
public
int
getBeginIndex() {
-
return
beginIndex;
-
}
-
public
void
setBeginIndex(
int
beginIndex) {
-
this
.beginIndex = beginIndex;
-
}
-
public
int
getEndIndex() {
-
return
endIndex;
-
}
-
public
void
setEndIndex(
int
endIndex) {
-
this
.endIndex = endIndex;
-
}
-
public
int
getBeginWeek() {
-
return
beginWeek;
-
}
-
public
void
setBeginWeek(
int
beginWeek) {
-
this
.beginWeek = beginWeek;
-
}
-
public
int
getEndWeek() {
-
return
endWeek;
-
}
-
public
void
setEndWeek(
int
endWeek) {
-
this
.endWeek = endWeek;
-
}
-
public
int
getId() {
-
return
id;
-
}
-
public
void
setId(
int
id) {
-
this
.id = id;
-
}
-
public
int
getCourseType() {
-
return
courseType;
-
}
-
public
void
setCourseType(
int
courseType) {
-
this
.courseType = courseType;
-
}
-
-
-
}
3、CourseInfoGallery
-
package
nd.leiyi.crims.gallery3D;
-
-
import
android.content.Context;
-
import
android.graphics.Camera;
-
import
android.graphics.Matrix;
-
import
android.util.AttributeSet;
-
import
android.view.MotionEvent;
-
import
android.view.View;
-
import
android.view.animation.Transformation;
-
import
android.widget.Gallery;
-
-
public
class
CourseInfoGallery
extends
Gallery {
-
-
private
Camera mCamera =
new
Camera();
-
private
int
mMaxRotationAngle =
60
;
-
private
int
mMaxZoom = -
60
;
-
private
int
mCoveflowCenter;
-
-
public
CourseInfoGallery(Context context) {
-
super
(context);
-
this
.setStaticTransformationsEnabled(
true
);
-
}
-
-
public
CourseInfoGallery(Context context, AttributeSet attrs) {
-
super
(context, attrs);
-
this
.setStaticTransformationsEnabled(
true
);
-
}
-
-
public
CourseInfoGallery(Context context, AttributeSet attrs,
int
defStyle) {
-
super
(context, attrs, defStyle);
-
this
.setStaticTransformationsEnabled(
true
);
-
}
-
-
public
int
getMaxRotationAngle() {
-
return
mMaxRotationAngle;
-
}
-
-
public
void
setMaxRotationAngle(
int
maxRotationAngle) {
-
mMaxRotationAngle = maxRotationAngle;
-
}
-
-
public
int
getMaxZoom() {
-
return
mMaxZoom;
-
}
-
-
public
void
setMaxZoom(
int
maxZoom) {
-
mMaxZoom = maxZoom;
-
}
-
-
private
int
getCenterOfCoverflow() {
-
return
(getWidth() - getPaddingLeft() - getPaddingRight()) /
2
-
+ getPaddingLeft();
-
}
-
-
private
static
int
getCenterOfView(View view) {
-
return
view.getLeft() + view.getWidth() /
2
;
-
}
-
-
protected
boolean
getChildStaticTransformation(View child, Transformation t) {
-
-
final
int
childCenter = getCenterOfView(child);
-
final
int
childWidth = child.getWidth();
-
int
rotationAngle =
0
;
-
-
t.clear();
-
t.setTransformationType(Transformation.TYPE_MATRIX);
-
-
if
(childCenter == mCoveflowCenter) {
-
transformImageBitmap(child, t,
0
);
-
}
else
{
-
rotationAngle = (
int
) (((
float
) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
-
if
(Math.abs(rotationAngle) > mMaxRotationAngle) {
-
rotationAngle = (rotationAngle <
0
) ? -mMaxRotationAngle
-
: mMaxRotationAngle;
-
}
-
transformImageBitmap(child, t, rotationAngle);
-
}
-
-
return
true
;
-
}
-
-
protected
void
onSizeChanged(
int
w,
int
h,
int
oldw,
int
oldh) {
-
mCoveflowCenter = getCenterOfCoverflow();
-
super
.onSizeChanged(w, h, oldw, oldh);
-
}
-
-
private
void
transformImageBitmap(View child, Transformation t,
-
int
rotationAngle) {
-
mCamera.save();
-
final
Matrix imageMatrix = t.getMatrix();
-
final
int
imageHeight = child.getLayoutParams().height;
-
final
int
imageWidth = child.getLayoutParams().width;
-
final
int
rotation = Math.abs(rotationAngle);
-
-
mCamera.translate(
0
.0f,
0
.0f,
100
.0f);
-
-
-
if
(rotation < mMaxRotationAngle) {
-
float
zoomAmount = (
float
) (mMaxZoom + (rotation *
1.5
));
-
mCamera.translate(
0
.0f,
0
.0f, zoomAmount);
-
}
-
-
mCamera.rotateY(rotationAngle);
-
mCamera.getMatrix(imageMatrix);
-
imageMatrix.preTranslate(-(imageWidth /
2
), -(imageHeight /
2
));
-
imageMatrix.postTranslate((imageWidth /
2
), (imageHeight /
2
));
-
mCamera.restore();
-
}
-
-
@Override
-
public
boolean
onInterceptTouchEvent(MotionEvent ev) {
-
-
if
(ev.getAction() == MotionEvent.ACTION_MOVE) {
-
return
true
;
-
}
else
{
-
return
false
;
-
}
-
}
-
-
-
-
}
4、CourseInfoAdapter
-
package
nd.leiyi.crims.adapter;
-
-
import
java.util.List;
-
-
import
nd.leiyi.crims.R;
-
import
nd.leiyi.crims.gallery3D.CourseInfoGallery;
-
import
nd.leiyi.crims.model.CourseInfo;
-
import
android.content.Context;
-
import
android.graphics.Color;
-
import
android.view.Gravity;
-
import
android.view.View;
-
import
android.view.ViewGroup;
-
import
android.widget.BaseAdapter;
-
import
android.widget.TextView;
-
-
public
class
CourseInfoAdapter
extends
BaseAdapter {
-
-
private
Context context;
-
private
TextView[] courseTextViewList;
-
private
int
screenWidth;
-
private
int
currentWeek;
-
public
CourseInfoAdapter(Context context, List<CourseInfo> courseList,
int
width,
int
currentWeek) {
-
super
();
-
this
.screenWidth = width;
-
this
.context = context;
-
this
.currentWeek = currentWeek;
-
createGalleryWithCourseList(courseList);
-
}
-
-
private
void
createGalleryWithCourseList(List<CourseInfo> courseList){
-
-
int
[] background = {R.drawable.course_info_blue, R.drawable.course_info_green,
-
R.drawable.course_info_red, R.drawable.course_info_red,
-
R.drawable.course_info_yellow};
-
this
.courseTextViewList =
new
TextView[courseList.size()];
-
for
(
int
i =
0
; i < courseList.size(); i ++)
-
{
-
final
CourseInfo course = courseList.get(i);
-
TextView textView =
new
TextView(context);
-
textView.setText(course.getCourseName() +
"@"
+ course.getClassRoom());
-
textView.setLayoutParams(
new
CourseInfoGallery.LayoutParams((screenWidth /
6
) *
3
, (screenWidth /
6
) *
3
));
-
textView.setTextColor(Color.WHITE);
-
textView.setGravity(Gravity.CENTER_VERTICAL);
-
textView.setPadding(
10
,
0
,
0
,
0
);
-
if
(course.getBeginWeek() <= currentWeek && course.getEndWeek() >= currentWeek &&
-
(course.getCourseType() == CourseInfo.ALL ||
-
(course.getCourseType() == CourseInfo.EVEN && currentWeek %
2
==
0
) ||
-
(course.getCourseType() == CourseInfo.ODD && currentWeek %
2
!=
0
)))
-
{
-
-
int
colorIndex = ((course.getBeginIndex() -
1
) *
8
+ course.getDay()) % (background.length -
1
);
-
textView.setBackgroundResource(background[colorIndex]);
-
}
-
else
-
{
-
textView.setBackgroundResource(R.drawable.course_info_light_grey);
-
}
-
textView.getBackground().setAlpha(
222
);
-
-
-
-
-
-
-
-
-
-
-
-
-
this
.courseTextViewList[i] = textView;
-
}
-
}
-
@Override
-
public
int
getCount() {
-
-
return
courseTextViewList.length;
-
}
-
-
@Override
-
public
Object getItem(
int
index) {
-
-
return
courseTextViewList[index];
-
}
-
-
@Override
-
public
long
getItemId(
int
arg0) {
-
-
return
arg0;
-
}
-
-
@Override
-
public
View getView(
int
position, View convertView, ViewGroup parent) {
-
-
return
courseTextViewList[position];
-
}
-
-
public
float
getScale(
boolean
focused,
int
offset) {
-
return
Math.max(
0
,
1
.0f / (
float
) Math.pow(
2
, Math.abs(offset)));
-
}
-
-
-
-
}
5、gallery-3d布局
-
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
-
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
-
android:layout_width
=
"fill_parent"
-
android:layout_height
=
"fill_parent"
-
android:orientation
=
"vertical"
>
-
-
<
nd.leiyi.crims.gallery3D.CourseInfoGallery
-
android:id
=
"@+id/course_info_gallery"
-
android:layout_width
=
"fill_parent"
-
android:layout_height
=
"wrap_content"
-
android:layout_centerInParent
=
"true"
-
/>
-
-
</
LinearLayout
>
6、gallery-3d-item
-
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
-
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
-
android:layout_width
=
"match_parent"
-
android:layout_height
=
"match_parent"
-
android:orientation
=
"vertical"
>
-
-
<
TextView
-
android:id
=
"@+id/course_info_gallery_item"
-
android:layout_width
=
"wrap_content"
-
android:layout_height
=
"wrap_content"
-
android:textColor
=
"#ffffff"
-
android:gravity
=
"center_vertical"
/>
-
-
</
LinearLayout
>
7、course_text_view_bg (课程格子背景)
-
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
-
<
layer-list
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
-
<
item
>
-
<
shape
>
-
<
solid
android:color
=
"#FFFFFF"
/>
-
<
stroke
-
android:width
=
"1dp"
-
android:color
=
"#a8abad"
/>
-
</
shape
>
-
</
item
>
-
<
item
-
android:right
=
"1dp"
-
android:bottom
=
"1dp"
>
-
<
shape
>
-
<
solid
android:color
=
"#FFFFFF"
/>
-
<
stroke
-
android:width
=
"1dp"
-
android:color
=
"#ffffff"
/>
-
</
shape
>
-
</
item
>
-
</
layer-list
>
8、course_table_last_colum(最后一列的背景,无边框)
-
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
-
<
layer-list
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
-
<
item
>
-
<
shape
>
-
<
solid
android:color
=
"#FFFFFF"
/>
-
<
stroke
-
android:width
=
"1dp"
-
android:color
=
"#a8abad"
/>
-
</
shape
>
-
</
item
>
-
<
item
-
android:bottom
=
"1dp"
>
-
<
shape
>
-
<
solid
android:color
=
"#FFFFFF"
/>
-
<
stroke
-
android:width
=
"1dp"
-
android:color
=
"#ffffff"
/>
-
</
shape
>
-
</
item
>
-
</
layer-list
>
还有一些布局文件就不贴了。代码太多了··,有兴趣的同学可以在github里下载我的工程···工程比较大,而且后台服务端程序我已经从云服务器上撤销了,所以跑不起来,我也不愿改代码了··
gitbub上的代码已删除
demo版:
超级课程表demo
《
超级课程表
》的
课表
界面
设计话不多说,直接看效果图下面我们来看《
超级课程表
的
界面
》这样的话,我们需要自定义三种视图:1.自定义 View方格背景
2.自定义 ViewGroup来装
课表
信息
3.自定义 TextView显示
课表
信息三者的关系是:自定义View提供背景,自定义ViewGroup提供装载
课表
视图 的容器,自定义TextView显示
课表
信息。首先我们先画背景!丛里往外进行设计1.自定
githu地址:https://github.com/longer96/CDTU
大一时针对我校开发的校园客户端,方便学生查
课表
、成绩、一卡通消费记录、失物招领等等。遵循MD设计原则,数据大多通过抓包获取,现开源(已屏蔽学校相关信息,怕被请回去喝茶)
Ps:第一次这么认真写文章,有不合理的地方希望大家提出 ...