Android中单行横向滑动的日历
16lz
2021-12-22
1.代码实现
直接上代码:
MainActivity.java
package com.zhoujian.horizontalcalendar.activity;import android.app.Activity;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.animation.AnimationUtils;import android.widget.AbsListView.LayoutParams;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.GridView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.ViewFlipper;import com.zhoujian.horizontalcalendar.R;import com.zhoujian.horizontalcalendar.adapter.DateAdapter;import com.zhoujian.horizontalcalendar.adapter.SpecialCalendar;import java.text.SimpleDateFormat;import java.util.Date;public class MainActivity extends Activity implements OnGestureListener { private static String TAG = "MainActivity"; private ViewFlipper flipper1 = null; private GridView gridView = null; private GestureDetector gestureDetector = null; private int year_c = 0; private int month_c = 0; private int day_c = 0; private int week_c = 0; private int week_num = 0; private String currentDate = ""; private DateAdapter dateAdapter; private int daysOfMonth = 0; private int dayOfWeek = 0; private int weeksOfMonth = 0; private SpecialCalendar sc = null; private boolean isLeapyear = false; private int selectPostion = 0; private String dayNumbers[] = new String[7]; private TextView tvDate; private int currentYear; private int currentMonth; private int currentWeek; private int currentDay; private int currentNum; public MainActivity() { 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]); currentYear = year_c; currentMonth = month_c; currentDay = day_c; sc = new SpecialCalendar(); getCalendar(year_c, month_c); week_num = getWeeksOfMonth(); currentNum = week_num; 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; } } } currentWeek = week_c; getCurrent(); } public int getWeeksOfMonth(int year, int month) { int preMonthRelax = 0; int dayFirst = getWhichDayOfWeek(year, month); int days = sc.getDaysOfMonth(sc.isLeapYear(year), month); if (dayFirst != 7) { preMonthRelax = dayFirst; } if ((days + preMonthRelax) % 7 == 0) { weeksOfMonth = (days + preMonthRelax) / 7; } else { weeksOfMonth = (days + preMonthRelax) / 7 + 1; } return weeksOfMonth; } public int getWhichDayOfWeek(int year, int month) { return sc.getWeekdayOfMonth(year, month); } public int getLastDayOfWeek(int year, int month) { return sc.getWeekDayOfLastMonth(year, month, sc.getDaysOfMonth(isLeapyear, month)); } 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; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvDate = (TextView) findViewById(R.id.tv_date); tvDate.setText(year_c + "年" + month_c + "月" + day_c + "日"); gestureDetector = new GestureDetector(this); flipper1 = (ViewFlipper) findViewById(R.id.flipper1); dateAdapter = new DateAdapter(this, currentYear, currentMonth,currentWeek, currentWeek == 1 ? true : false); addGridView(); dayNumbers = dateAdapter.getDayNumbers(); gridView.setAdapter(dateAdapter); selectPostion = dateAdapter.getTodayPosition(); gridView.setSelection(selectPostion); flipper1.addView(gridView, 0); } private void addGridView() { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 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 OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return MainActivity.this.gestureDetector.onTouchEvent(event); } }); gridView.setOnItemClickListener(new 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(); tvDate.setText(dateAdapter.getCurrentYear(selectPostion) + "年"+ dateAdapter.getCurrentMonth(selectPostion) + "月"+ dayNumbers[position] + "日"); } }); gridView.setLayoutParams(params); } @Override protected void onPause() { super.onPause(); } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } 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; } } @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); tvDate.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); 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); tvDate.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); return true; } return false; }}
DateAdapter.java
package com.zhoujian.horizontalcalendar.adapter;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.TextView;import com.zhoujian.horizontalcalendar.R;import java.text.SimpleDateFormat;import java.util.Date;public class DateAdapter extends BaseAdapter{ private boolean isLeapyear = false; private int daysOfMonth = 0; private int dayOfWeek = 0; private int lastDaysOfMonth = 0; private Context context; private SpecialCalendar sc = null; private String[] dayNumber = new String[7]; private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d"); private String sysDate = ""; private String sys_year = ""; private String sys_month = ""; private String sys_day = ""; private String currentYear = ""; private String currentMonth = ""; private String currentWeek = ""; private String currentDay = ""; private int weeksOfMonth; private int clickTemp = -1; private boolean isStart; public void setSeclection(int position) { clickTemp = position; } public DateAdapter() { Date date = new Date(); sysDate = sdf.format(date); sys_year = sysDate.split("-")[0]; sys_month = sysDate.split("-")[1]; sys_day = sysDate.split("-")[2]; } public DateAdapter(Context context, int year_c, int month_c, int week_c,boolean isStart) { this(); this.context = context; this.isStart = isStart; sc = new SpecialCalendar(); currentYear = String.valueOf(year_c); currentMonth = String.valueOf(month_c); currentDay = String.valueOf(sys_day); getCalendar(Integer.parseInt(currentYear),Integer.parseInt(currentMonth)); currentWeek = String.valueOf(week_c); getWeek(Integer.parseInt(currentYear), Integer.parseInt(currentMonth),Integer.parseInt(currentWeek)); } public int getTodayPosition() { int todayWeek = sc.getWeekDayOfLastMonth(Integer.parseInt(sys_year), Integer.parseInt(sys_month), Integer.parseInt(sys_day)); if (todayWeek == 7) { clickTemp = 0; } else { clickTemp = todayWeek; } return clickTemp; } public int getCurrentMonth(int position) { int thisDayOfWeek = sc.getWeekdayOfMonth(Integer.parseInt(currentYear), Integer.parseInt(currentMonth)); if (isStart) { if (thisDayOfWeek != 7) { if (position < thisDayOfWeek) { return Integer.parseInt(currentMonth) - 1 == 0 ? 12 : Integer.parseInt(currentMonth) - 1; } else { return Integer.parseInt(currentMonth); } } else { return Integer.parseInt(currentMonth); } } else { return Integer.parseInt(currentMonth); } } public int getCurrentYear(int position) { int thisDayOfWeek = sc.getWeekdayOfMonth(Integer.parseInt(currentYear), Integer.parseInt(currentMonth)); if (isStart) { if (thisDayOfWeek != 7) { if (position < thisDayOfWeek) { return Integer.parseInt(currentMonth) - 1 == 0 ? Integer .parseInt(currentYear) - 1 : Integer .parseInt(currentYear); } else { return Integer.parseInt(currentYear); } } else { return Integer.parseInt(currentYear); } } else { return Integer.parseInt(currentYear); } } public void getCalendar(int year, int month) { isLeapyear = sc.isLeapYear(year); daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); dayOfWeek = sc.getWeekdayOfMonth(year, month); lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1); } public void getWeek(int year, int month, int week) { for (int i = 0; i < dayNumber.length; i++) { if (dayOfWeek == 7) { dayNumber[i] = String.valueOf((i + 1) + 7 * (week - 1)); } else { if (week == 1) { if (i < dayOfWeek) { dayNumber[i] = String.valueOf(lastDaysOfMonth - (dayOfWeek - (i + 1))); } else { dayNumber[i] = String.valueOf(i - dayOfWeek + 1); } } else { dayNumber[i] = String.valueOf((7 - dayOfWeek + 1 + i) + 7 * (week - 2)); } } } } public String[] getDayNumbers() { return dayNumber; } 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; } public void getDayInWeek(int year, int month) { } @Override public int getCount() { return dayNumber.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder; if (convertView == null) { holder = new Holder(); convertView = LayoutInflater.from(context).inflate( R.layout.item_calendar, null); holder.tv = (TextView) convertView.findViewById(R.id.tv_calendar); holder.tv_month = (TextView) convertView .findViewById(R.id.tv_month); holder.ll_data = (LinearLayout) convertView .findViewById(R.id.ll_data); holder.view_line = (View) convertView.findViewById(R.id.view_line); convertView.setTag(holder); } else { holder = (Holder) convertView.getTag(); } holder.tv.setText(dayNumber[position]); holder.tv_month.setText(getCurrentMonth(position)+"."); if (clickTemp == position) { holder.ll_data.setSelected(true); holder.view_line.setVisibility(View.VISIBLE); } else { holder.ll_data.setSelected(false); holder.view_line.setVisibility(View.INVISIBLE); } return convertView; } class Holder { LinearLayout ll_data; TextView tv; TextView tv_month; View view_line; }}
SpecialCalendar.java
package com.zhoujian.horizontalcalendar.adapter;import java.util.Calendar;public class SpecialCalendar { private int daysOfMonth = 0; private int dayOfWeek = 0; private int eachDayOfWeek = 0; public boolean isLeapYear(int year) { if (year % 100 == 0 && year % 400 == 0) { return true; } else if (year % 100 != 0 && year % 4 == 0) { return true; } return false; } public int getDaysOfMonth(boolean isLeapyear, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: daysOfMonth = 31; break; case 4: case 6: case 9: case 11: daysOfMonth = 30; break; case 2: if (isLeapyear) { daysOfMonth = 29; } else { daysOfMonth = 28; } } return daysOfMonth; } public int getWeekdayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, 1); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1; return dayOfWeek; } public int getWeekDayOfLastMonth(int year, int month, int day) { Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, day); eachDayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1; return eachDayOfWeek; }}
2.运行截图
3.源码下载
源码下载:https://github.com/zeke123/HorizontalCalendar
更多相关文章
- Android网格布局实现--recyclerview
- Android(安卓)代码混淆异常 transformClassesAndResourcesWithPr
- Android动态刷新listview中的数据
- android 异步图片处理 工具类
- Android图片圆角 用简单的方法实现
- android中隐藏以及显示软键盘代码
- Android(安卓)Handler(七)
- Android如何获取屏幕分辨率的例子
- Android(安卓)动画 Activity切换动画