android自定义课程表

这是第一次写博客,之前都是上传Demo。因为公司与学校合作,所以要实现展示课程表的功能,此前在网上看过好多课表的例子,总觉得不适合自己的业务需要,所以自己写了一个自定义的视图来实现课程表功能。这个小练习功能暂时不全,只展示了周视图。日视图代码已经写好,需要的请自己实现(如果要实现日视图和周视图切换展示,请记得视图的重绘)。要是其中有错误或有更好的思路还望各位赐教。

图片截取不完整,如需观看效果,请下载Demo



import java.util.ArrayList;
import java.util.List;
import com.example.courseview.R;
import com.example.courseview.CourseRootMode.CourseList;
import com.example.courseview.CourseRootMode.CourseList.Rows;
import com.example.courseview.CourseRootMode.CourseList.Rows.Week_day1;
import com.example.courseview.CourseRootMode.CourseList.Rows.Week_day2;
import com.example.courseview.CourseRootMode.CourseList.Rows.Week_day3;
import com.example.courseview.CourseRootMode.CourseList.Rows.Week_day4;
import com.example.courseview.CourseRootMode.CourseList.Rows.Week_day5;
import com.example.courseview.CourseRootMode.CourseList.Rows.Week_day6;
import com.example.courseview.CourseRootMode.CourseList.Rows.Week_day7;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RelativeLayout;


/**
 * 周视图
 * @author xing
 *
 */
@SuppressLint("InflateParams")
public class CustomWeekCourseView extends LinearLayout {
private final static int weekNum = 8; // 每周七天(前边显示月份)
private final static int sectionNum = 14; // 每周上课结束
private static int sectionHeight = 55; // 每节课的高度
private TextView sectionText; //
private LinearLayout sectionLayout; // 节父控件
private LinearLayout contentLayout;// 内容控件
private LinearLayout courseLayout; // 课表父控件
private final static String[] sectionName = { "1", "2", "3", "4", "午休", "5", "6", "7", "8", "晚休", "9", "10", "11",
"12" };
private Week_day1 week_day_1;
private Week_day2 week_day_2;
private Week_day3 week_day_3;
private Week_day4 week_day_4;
private Week_day5 week_day_5;
private Week_day6 week_day_6;
private Week_day7 week_day_7;
private List courseData[] = null;
private CourseList courseList = null;// 返回课程列表数据
private Context context;
private List<Course> list = null; // 保存每一天的课程信息
/**
* 上午
*/
private LinearLayout morningLayout;
/**
* 午休
*/
private LinearLayout wuxiuLayout;
/**
* 下午
*/
private LinearLayout afterLayout;


/**
* 晚休
*/
private LinearLayout wanxiuLayout;
/**
* 晚上
*/
private LinearLayout nightLayout;


private int selectIndex =0; //默认第一天?


public CustomWeekCourseView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public CustomWeekCourseView(Context context) {
this(context, null);
}


@SuppressLint("NewApi")
public CustomWeekCourseView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}


public void setData(CourseList courseList,int selectIndex) {
this.courseList = courseList;
this.selectIndex = selectIndex;
courseData = new ArrayList[7];
contentLayout = new LinearLayout(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 0);
contentLayout.removeAllViews();
contentLayout.setLayoutParams(lp);
contentLayout.setOrientation(HORIZONTAL);
initSectionView(courseList.getRows());
}


/**
* 初始化左侧节视图
*/
private void initSectionView(Rows rows) {
if (rows != null) {
getData(rows);
}
sectionLayout = new LinearLayout(context);
LayoutParams mm = new LayoutParams(ScreenUtil.getScreenWidth(context) / weekNum, LayoutParams.MATCH_PARENT);
mm.setMargins(0, 0, 0, 0);
sectionLayout.setLayoutParams(mm);
sectionLayout.setOrientation(VERTICAL);
sectionLayout.setWeightSum(1.0f);
sectionLayout.setGravity(Gravity.TOP);
for (int i = 0; i < sectionNum; i++) {
LinearLayout sectionItemLayout = new LinearLayout(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(sectionHeight));
sectionItemLayout.setLayoutParams(params);
sectionItemLayout.setGravity(Gravity.CENTER);
sectionItemLayout.setBackgroundResource(R.drawable.linearlayout_bg_frame);
sectionText = new TextView(context);
sectionText.setTextColor(ContextCompat.getColor(context, android.R.color.black));
sectionText.setTextSize(15);
sectionText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
sectionText.setGravity(Gravity.CENTER);
sectionText.setText(sectionName[i]);
sectionItemLayout.addView(sectionText);
sectionLayout.addView(sectionItemLayout);
}
contentLayout.addView(sectionLayout);
initContentView();
addView(contentLayout);
}


/**
* 初始化课程表视图
*/
private void initContentView() {
courseLayout = new LinearLayout(context);
LayoutParams mm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mm.setMargins(0, -1, 0, 0);
courseLayout.removeAllViews();
courseLayout.setLayoutParams(mm);
courseLayout.setWeightSum(2.0f);
courseLayout.setOrientation(VERTICAL);
morningLayout = new LinearLayout(context);
morningLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(sectionHeight * 4)));
morningLayout.setBackgroundResource(R.drawable.linearlayout_bg_frame);
morningLayout.setOrientation(HORIZONTAL);
afterLayout = new LinearLayout(context);
afterLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(sectionHeight * 4)));
afterLayout.setBackgroundResource(R.drawable.linearlayout_bg_frame);
afterLayout.setOrientation(HORIZONTAL);
wuxiuLayout = new LinearLayout(context);
wuxiuLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(sectionHeight)));
nightLayout = new LinearLayout(context);
nightLayout.setOrientation(HORIZONTAL);
nightLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(sectionHeight * 4)));
nightLayout.setBackgroundResource(R.drawable.linearlayout_bg_frame);
wanxiuLayout = new LinearLayout(context);
wanxiuLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(sectionHeight)));
courseLayout.addView(morningLayout);
courseLayout.addView(wuxiuLayout);
courseLayout.addView(afterLayout);
courseLayout.addView(wanxiuLayout);
courseLayout.addView(nightLayout);
contentLayout.addView(courseLayout);
//显示周视图
initWeekDataView();
//也可以现实日视图initDayView();
}


/**
* 初始化休息视�?
*/
private void initRestView() {
TextView wuxiuText = initRestText();
wuxiuLayout.addView(wuxiuText);
wuxiuLayout.setGravity(Gravity.CENTER_VERTICAL);
TextView wanxiuText = initRestText();
wanxiuLayout.addView(wanxiuText);
wanxiuLayout.setGravity(Gravity.CENTER_VERTICAL);
if (courseList != null && courseList.getStatic_info() != null) {
wuxiuText.setText(courseList.getStatic_info().getXiu_msg_1());
wanxiuText.setText(courseList.getStatic_info().getXiu_msg_2());
}else{
wuxiuText.setText("");
wanxiuText.setText("");
}
}


@SuppressLint("RtlHardcoded")
private TextView initRestText() {
TextView restText = new TextView(context);
restText.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
LayoutParams ll = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
ll.setMargins(dip2px(5), 0, 0, 0);
return restText;
}


/**
* 初始化周视图
*/
private void initWeekDataView() {
/**
* 清空之前父布局
*/
morningLayout.removeAllViews();
afterLayout.removeAllViews();
nightLayout.removeAllViews();
for (int i = 0; i < weekNum - 1; i++) {
RelativeLayout morningDayLayout = new RelativeLayout(context);
LayoutParams lp = new LayoutParams(ScreenUtil.getScreenWidth(context) / weekNum, LayoutParams.MATCH_PARENT);
morningDayLayout.setLayoutParams(lp);
morningDayLayout.setBackgroundResource(R.drawable.linearlayout_bg_frame);
morningLayout.addView(morningDayLayout);


RelativeLayout afterDayLayout = new RelativeLayout(context);
afterDayLayout.setBackgroundResource(R.drawable.linearlayout_day_bg_frame);
afterDayLayout.setLayoutParams(lp);
afterLayout.addView(afterDayLayout);


RelativeLayout nightDayLayout = new RelativeLayout(context);
nightDayLayout.setBackgroundResource(R.drawable.linearlayout_day_bg_frame);
nightDayLayout.setLayoutParams(lp);
nightLayout.addView(nightDayLayout);

list = courseData[i];
if (list != null && list.size() != 0) {
for (int j = 0; j < list.size(); j++) {
final Course c = list.get(j);
View inflater = LayoutInflater.from(context).inflate(R.layout.course_monty_item, null);
TextView name = (TextView) inflater.findViewById(R.id.tvCourseName);
TextView address = (TextView) inflater.findViewById(R.id.tvCourseAddress);
inflater.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
dialogShow(c);
}
});
name.setText(c.getName());
address.setText(c.getRoom());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
dip2px(sectionHeight * c.getStep()));
inflater.setLayoutParams(params);
if (j > 0) {
if (c.getStart() <= 4) {
if (c.getStart() > 1) {
params.setMargins(0, dip2px((c.getStart() - 1) * sectionHeight), 0, 0);
morningDayLayout.addView(inflater);
} else {
morningDayLayout.addView(inflater);
}
} else if (c.getStart() >= 5 && c.getStart() <= 8) {
if (c.getStart() > 5) {
params.setMargins(0, dip2px((c.getStart() - 5) * sectionHeight), 0, 0);
afterDayLayout.addView(inflater);
} else {
afterDayLayout.addView(inflater);
}
} else if (c.getStart() >= 9) {
if (c.getStart() > 9) {
params.setMargins(0, dip2px((c.getStart() - 9) * sectionHeight), 0, 0);
nightDayLayout.addView(inflater);
} else {
nightDayLayout.addView(inflater);
}
}
} else {
if (c.getStart() <= 4) {
params.setMargins(0, dip2px((c.getStart() - 1) * sectionHeight), 0, 0);
morningDayLayout.addView(inflater);
} else if (c.getStart() >= 5 && c.getStart() <= 8) {
params.setMargins(0, dip2px((c.getStart() - 5) * sectionHeight), 0, 0);
afterDayLayout.addView(inflater);
} else if (c.getStart() >= 9) {
params.setMargins(0, dip2px((c.getStart() - 9) * sectionHeight), 0, 0);
nightDayLayout.addView(inflater);
}
}
}
}
morningDayLayout.addView(getLine());
afterDayLayout.addView(getLine());
nightDayLayout.addView(getLine());
}
initRestView();
}

/**
* 初始化天视图(暂未使用)
*/
private void initDayView() {
/**
* 清空之前父布局
*/
morningLayout.removeAllViews();
afterLayout.removeAllViews();
nightLayout.removeAllViews();
// 上午课程视图
RelativeLayout morningDayLayout = new RelativeLayout(context);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, dip2px(sectionHeight * 4));
morningDayLayout.setBackgroundResource(R.drawable.linearlayout_bg_frame);
morningDayLayout.setLayoutParams(lp);
morningLayout.addView(morningDayLayout);


// 下午课表视图
RelativeLayout afterDayLayout = new RelativeLayout(context);
afterDayLayout.setBackgroundResource(R.drawable.linearlayout_day_bg_frame);
afterDayLayout.setLayoutParams(lp);
afterLayout.addView(afterDayLayout);


// 晚上课表视图
RelativeLayout nightDayLayout = new RelativeLayout(context);
nightDayLayout.setBackgroundResource(R.drawable.linearlayout_day_bg_frame);
nightDayLayout.setLayoutParams(lp);
nightLayout.addView(nightDayLayout);
setDayCourseData(morningDayLayout, afterDayLayout, nightDayLayout);
initRestView();
}


/**
* 初始化天课表视图
*/
private void setDayCourseData(RelativeLayout morningDayLayout, RelativeLayout afterDayLayout,
RelativeLayout nightDayLayout) {
morningDayLayout.removeAllViews();
afterDayLayout.removeAllViews();
nightDayLayout.removeAllViews();
RelativeLayout morningItemLayout = morningDayLayout;
RelativeLayout afterItemLayout = afterDayLayout;
RelativeLayout nightItemLayout = nightDayLayout;
morningItemLayout.addView(getLine());
afterItemLayout.addView(getLine());
nightItemLayout.addView(getLine());
List<Course> list = courseData[selectIndex]; // (周一)
if (list != null && list.size() != 0) {
for (int j = 0; j < list.size(); j++) {
Course c = list.get(j);
View inflater = LayoutInflater.from(context).inflate(R.layout.course_day_item, null);
TextView name = (TextView) inflater.findViewById(R.id.tvCourseName_day);
TextView teach = (TextView) inflater.findViewById(R.id.tvCourseTeacher_day);
TextView address = (TextView) inflater.findViewById(R.id.tvCourseAddress_day);
teach.setText(c.getTeach());
name.setText(c.getName());
address.setText(c.getRoom());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
dip2px(sectionHeight * c.getStep()));
inflater.setLayoutParams(params);
if (j > 0) {
if (c.getStart() <= 4) {
if (c.getStart() > 1) {
params.setMargins(0, dip2px((c.getStart() - 1) * sectionHeight), 0, 0);
morningItemLayout.addView(inflater);
} else {
morningItemLayout.addView(inflater);
}
} else if (c.getStart() >= 5 && c.getStart() <= 8) {
if (c.getStart() > 5) {
params.setMargins(0, dip2px((c.getStart() - 5) * sectionHeight), 0, 0);
afterItemLayout.addView(inflater);
} else {
afterItemLayout.addView(inflater);
}
} else if (c.getStart() >= 9) {
if (c.getStart() > 9) {
params.setMargins(0, dip2px((c.getStart() - 9) * sectionHeight), 0, 0);
nightItemLayout.addView(inflater);
} else {
nightItemLayout.addView(inflater);
}
}
} else {
if (c.getStart() <= 4) {
params.setMargins(0, dip2px((c.getStart() - 1) * sectionHeight), 0, 0);
morningItemLayout.addView(inflater);
} else if (c.getStart() >= 5 && c.getStart() <= 8) {
params.setMargins(0, dip2px((c.getStart() - 5) * sectionHeight), 0, 0);
afterItemLayout.addView(inflater);
} else if (c.getStart() >= 9) {
params.setMargins(0, dip2px((c.getStart() - 9) * sectionHeight), 0, 0);
nightItemLayout.addView(inflater);
}
}
}
}
}


// 展示课程信息
public void dialogShow(Course value) {
Toast.makeText(context, value.getName(), Toast.LENGTH_SHORT).show();
}


/**
* 横向分割线
*/
private View getLine() {
View view = LayoutInflater.from(context).inflate(R.layout.title_line, null);
RelativeLayout.LayoutParams mm = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, dip2px(0.3f));
mm.setMargins(0, dip2px(sectionHeight * 2), 0, 0);
view.setLayoutParams(mm);
return view;
}


public void getData(Rows rows) {
List<Course> list1 = new ArrayList<Course>();
if (rows.getWeek_day_1().size() > 0) {
for (int i = 0; i < rows.getWeek_day_1().size(); i++) {
week_day_1 = rows.getWeek_day_1().get(i);
Course c1 = new Course(week_day_1.getSubtitle(), week_day_1.getPlace(),
Integer.parseInt(week_day_1.getSort()), Integer.parseInt(week_day_1.getLession_num()),
week_day_1.getTeacher(), week_day_1.getId(), week_day_1.getStarttime());
list1.add(c1);
}
}
courseData[0] = list1;
List<Course> list2 = new ArrayList<Course>();
if (rows.getWeek_day_2().size() > 0) {
for (int i = 0; i < rows.getWeek_day_2().size(); i++) {
week_day_2 = rows.getWeek_day_2().get(i);
Course c2 = new Course(week_day_2.getSubtitle(), week_day_2.getPlace(),
Integer.parseInt(week_day_2.getSort()), Integer.parseInt(week_day_2.getLession_num()),
week_day_2.getTeacher(), week_day_2.getId(), week_day_2.getStarttime());
list2.add(c2);
}


}
courseData[1] = list2;
List<Course> list3 = new ArrayList<Course>();
if (rows.getWeek_day_3().size() > 0) {


for (int i = 0; i < rows.getWeek_day_3().size(); i++) {
week_day_3 = rows.getWeek_day_3().get(i);
Course c3 = new Course(week_day_3.getSubtitle(), week_day_3.getPlace(),
Integer.parseInt(week_day_3.getSort()), Integer.parseInt(week_day_3.getLession_num()),
week_day_3.getTeacher(), week_day_3.getId(), week_day_3.getStarttime());
list3.add(c3);
}


}
courseData[2] = list3;
List<Course> list4 = new ArrayList<Course>();
if (rows.getWeek_day_4().size() > 0) {


for (int i = 0; i < rows.getWeek_day_4().size(); i++) {
week_day_4 = rows.getWeek_day_4().get(i);
Course c4 = new Course(week_day_4.getSubtitle(), week_day_4.getPlace(),
Integer.parseInt(week_day_4.getSort()), Integer.parseInt(week_day_4.getLession_num()),
week_day_4.getTeacher(), week_day_4.getId(), week_day_4.getStarttime());
list4.add(c4);
}


}
courseData[3] = list4;


List<Course> list5 = new ArrayList<Course>();
if (rows.getWeek_day_5().size() > 0) {


for (int i = 0; i < rows.getWeek_day_5().size(); i++) {
week_day_5 = rows.getWeek_day_5().get(i);
Course c5 = new Course(week_day_5.getSubtitle(), week_day_5.getPlace(),
Integer.parseInt(week_day_5.getSort()), Integer.parseInt(week_day_5.getLession_num()),
week_day_5.getTeacher(), week_day_5.getId(), week_day_5.getStarttime());
list5.add(c5);
}
}
courseData[4] = list5;
List<Course> list6 = new ArrayList<Course>();
if (rows.getWeek_day_6().size() > 0) {
for (int i = 0; i < rows.getWeek_day_6().size(); i++) {
week_day_6 = rows.getWeek_day_6().get(i);
Course c6 = new Course(week_day_6.getSubtitle(), week_day_6.getPlace(),
Integer.parseInt(week_day_6.getSort()), Integer.parseInt(week_day_6.getLession_num()),
week_day_6.getTeacher(), week_day_6.getId(), week_day_6.getStarttime());
list6.add(c6);
}


}
courseData[5] = list6;


List<Course> list7 = new ArrayList<Course>();
if (rows.getWeek_day_7().size() > 0) {
for (int i = 0; i < rows.getWeek_day_7().size(); i++) {
week_day_7 = rows.getWeek_day_7().get(i);
Course c7 = new Course(week_day_7.getSubtitle(), week_day_7.getPlace(),
Integer.parseInt(week_day_7.getSort()), Integer.parseInt(week_day_7.getLession_num()),
week_day_7.getTeacher(), week_day_7.getId(), week_day_7.getStarttime());
list7.add(c7);
}
}
courseData[6] = list7;
}


/**
* 转换dp
*
* @param dpValue
* @return
*/
public int dip2px(float dpValue) {
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

以上只是课表视图代码,如果想了解整个demo,请到我的资源中下载。

源码地址:http://download.csdn.net/detail/u012908087/9727850

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值