最近有个控件是实现和去哪儿和阿里旅行的app的选择日历效果,反编译没有效果的情况下我自己实现了个,大致的原理是:
上面是产品需要实现的效果,我看了下不就是一个ListView+gridView就能实现么,方案有了,自定义的CalendarView实现对日期的计算,然后可以按ios显示的风格显示日历
public class MyCalendar extends LinearLayout {
private static Context context;
private Date theInDay;
private String inday = "", outday = "";
private List gvList;//存放天
private CalGridViewAdapter calAdapter;
private OnDaySelectListener callBack;//回调函数
public MyCalendar(Context context) {
super(context);
this.context = context;
}
public MyCalendar(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void setInDay(String inday) {
this.inday = inday;
}
public void setOutDay(String outday) {
this.outday = outday;
}
public void setTheDay(Date dateIn) {
this.theInDay = dateIn;
init();
}
/**
* 初始化日期以及view等控件
*/
private void init() {
gvList = new ArrayList();//存放天
Calendar cal = Calendar.getInstance();//获取日历实例
cal.setTime(theInDay);//cal设置为当天的
cal.set(Calendar.DATE, 1);//cal设置当前day为当前月第一天
int tempSum = countNeedHowMuchEmpety(cal);//获取当前月第一天为星期几
int dayNumInMonth = getDayNumInMonth(cal);//获取当前月有多少天
setGvListData(tempSum, dayNumInMonth, cal.get(Calendar.YEAR) + "-" + getMonth((cal.get(Calendar.MONTH) + 1)));
View view = LayoutInflater.from(context).inflate(R.layout.comm_calendar, this, true);//获取布局,开始初始化
TextView tv_year = (TextView) view.findViewById(R.id.tv_year);
if (cal.get(Calendar.YEAR) > new Date().getYear()) {
tv_year.setVisibility(View.VISIBLE);
tv_year.setText(cal.get(Calendar.YEAR) + "年");
}
TextView tv_month = (TextView) view.findViewById(R.id.tv_month);
tv_month.setText(String.valueOf(theInDay.getMonth() + 1) + "月");
MyGridView gv = (MyGridView) view.findViewById(R.id.gv_calendar);
calAdapter = new CalGridViewAdapter(context,gvList, inday, outday);
gv.setAdapter(calAdapter);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View arg1, int position, long arg3) {
String choiceDay = (String) adapterView.getAdapter().getItem(position);
String[] date = choiceDay.split(",");
String day = date[1];
if (!" ".equals(day)) {
if (Integer.parseInt(day) < 10) {
day = "0" + date[1];
}
choiceDay = date[0] + "-" + day;
if (callBack != null) {//调用回调函数回调数据
callBack.onDaySelectListener(arg1, choiceDay);
}
}
}
});
}
/**
* 为gridview中添加需要展示的数据
*
* @param tempSum
* @param dayNumInMonth
*/
private void setGvListData(int tempSum, int dayNumInMonth, String YM) {
if (gvList!=null){
gvList.clear();
for (int i = 0; i < tempSum; i++) {
gvList.add(" , ");
}
for (int j = 1; j
关注
打赏
