Android周日历可滑动可选择日期

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ayrascal/article/details/78342568

实现思路

周日历的左右滑动使用了ViewFlipper组件  调用addView(view,index)方法  这里的view  我用了GridView  配合GestureDetector类识别左右滑动的手势  默认显示本周的日期并选中当前这一天   

实现方法:

首先:获取当天日期

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
currentDate = sdf.format(date);
year_c = Integer.parseInt(currentDate.split("-")[0]);
month_c = Integer.parseInt(currentDate.split("-")[1]);
day_c = Integer.parseInt(currentDate.split("-")[2]);

通过获取到的年和月份  判断是否为闰年  这个月的天数 第一天为周几

public void getCalendar(int year, int month) {
isLeapyear = sc.isLeapYear(year); // 是否为闰年
daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的总天数
dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天为星期几
}

判断这个月一共有几周

public int getWeeksOfMonth() {
int preMonthRelax = 0;
if (dayOfWeek != 7) {
preMonthRelax = dayOfWeek;
}
if ((daysOfMonth + preMonthRelax) % 7 == 0) {
weeksOfMonth = (daysOfMonth + preMonthRelax) / 7;
} else {
weeksOfMonth = (daysOfMonth + preMonthRelax) / 7 + 1;
}
return weeksOfMonth;
}

判断现在是第几周(周日算一周的开始)

if (dayOfWeek == 7) {
week_c = currentDay / 7 + 1;
} else {
if (currentDay <= (7 - dayOfWeek)) {
week_c = 1;
} else {
if ((currentDay - (7 - dayOfWeek)) % 7 == 0) {
week_c = (currentDay - (7 - dayOfWeek)) / 7 + 1;
} else {
week_c = (currentDay - (7 - dayOfWeek)) / 7 + 2;
}
}
}

计算当前的年月日

public void getCurrent() {
if (currentWeek > currentNum) {
if (currentMonth + 1 <= 12) {
currentMonth++;
} else {
currentMonth = 1;
currentYear++;
}
currentWeek = 1;
currentNum = getWeeksOfMonth(currentYear, currentMonth);
} else if (currentWeek == currentNum) {
if (getLastDayOfWeek(currentYear, currentMonth) == 6) {
} else {
if (currentMonth + 1 <= 12) {
currentMonth++;
} else {
currentMonth = 1;
currentYear++;
}
currentWeek = 1;
currentNum = getWeeksOfMonth(currentYear, currentMonth);
}
} else if (currentWeek < 1) {
if (currentMonth - 1 >= 1) {
currentMonth--;
} else {
currentMonth = 12;
currentYear--;
}
currentNum = getWeeksOfMonth(currentYear, currentMonth);
currentWeek = currentNum - 1;
}
}

加载GridView

private void addGridView() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
gridView = new GridView(this);
gridView.setNumColumns(7);
gridView.setGravity(Gravity.CENTER_VERTICAL);
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
gridView.setVerticalSpacing(1);
gridView.setHorizontalSpacing(1);
gridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return MySchedulingActivity.this.gestureDetector.onTouchEvent(event);
}
});

gridView.setLayoutParams(params);

可设置点击事件
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(TAG, "day:" + dayNumbers[position]);
selectPostion = position;
dateAdapter.setSeclection(position);
dateAdapter.notifyDataSetChanged();
tv_date.setText(dateAdapter.getCurrentYear(selectPostion) + "-"
+ dateAdapter.getCurrentMonth(selectPostion) + "-"
+ dayNumbers[position] );
tvtime = tv_date.getText()+"";
tvtime = DateUtils.getDateStrFormat(tvtime);
Log.d("tvtime",tvtime+"------");
}


最后 可以设置日历的左右滑动

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int gvFlag = 0;
if (e1.getX() - e2.getX() > 80) {
// 向左滑
addGridView();
currentWeek++;
getCurrent();
dateAdapter = new DateAdapter(this, currentYear, currentMonth,
currentWeek, currentWeek == 1 ? true : false);
dayNumbers = dateAdapter.getDayNumbers();
gridView.setAdapter(dateAdapter);
tv_date.setText(dateAdapter.getCurrentYear(selectPostion) + "-"
+ dateAdapter.getCurrentMonth(selectPostion) + "-"
+ dayNumbers[selectPostion] );

gvFlag++;
flipper1.addView(gridView, gvFlag);
dateAdapter.setSeclection(selectPostion);
this.flipper1.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.flipper1.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.flipper1.showNext();
flipper1.removeViewAt(0);
tvtime = tv_date.getText()+"";
tvtime = DateUtils.getDateStrFormat(tvtime);
return true;

} else if (e1.getX() - e2.getX() < -80) {
addGridView();
currentWeek--;
getCurrent();
dateAdapter = new DateAdapter(this, currentYear, currentMonth,
currentWeek, currentWeek == 1 ? true : false);
dayNumbers = dateAdapter.getDayNumbers();
gridView.setAdapter(dateAdapter);
tv_date.setText(dateAdapter.getCurrentYear(selectPostion) + "-"
+ dateAdapter.getCurrentMonth(selectPostion) + "-"
+ dayNumbers[selectPostion] );


gvFlag++;
flipper1.addView(gridView, gvFlag);
dateAdapter.setSeclection(selectPostion);
this.flipper1.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.flipper1.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.flipper1.showPrevious();
flipper1.removeViewAt(0);
int i = 0;
tvtime
return true;
}
return false;
}


效果图



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页