// MonthItemView
public class MonthItemView extends AppCompatTextView {
private MonthItem item;
public MonthItemView(Context context) {
super(context);
init();
}
public MonthItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setBackgroundColor(Color.WHITE);
}
public MonthItem getItem() {
return item;
}
// 날짜 입력
public void setItem(MonthItem item) {
this.item = item;
int day = item.getDay();
if (day != 0) {
setText(String.valueOf(day));
} else {
setText("");
}
}
}
// MonthAdapte
package app.gotogether.com.group_page;
import android.content.Context;
import android.graphics.Color;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import java.util.Calendar;
/**
* 어댑터 객체 정의
*
* @author Mike
*
*/
public class MonthAdapter extends BaseAdapter {
public static final String TAG = "MonthAdapter";
//private LayoutInflater layoutInflater;
Context mContext;
public static int oddColor = Color.rgb(225, 225, 225); // 색지정
public static int headColor = Color.rgb(12, 32, 158);
private int selectedPosition = -1;
private MonthItem[] items;
private int countColumn = 7;
int mStartDay;
int startDay;
int curYear; // 현재 년도
int curMonth; // 현재 월
int firstDay; // 첫날
int lastDay; // 마지막날
Calendar mCalendar; // 달력
boolean recreateItems = false;
public MonthAdapter(Context context) {
super();
mContext = context;
init();
}
public MonthAdapter(Context context, AttributeSet attrs) {
super();
mContext = context;
init();
}
private void init() {
items = new MonthItem[7 * 6];
mCalendar = Calendar.getInstance();
recalculate();
resetDayNumbers();
}
// 달력 날짜 계산
public void recalculate() {
// set to the first day of the month
mCalendar.set(Calendar.DAY_OF_MONTH, 1);
// get week day
int dayOfWeek = mCalendar.get(Calendar.DAY_OF_WEEK);
firstDay = getFirstDay(dayOfWeek);
Log.d(TAG, "firstDay : " + firstDay);
mStartDay = mCalendar.getFirstDayOfWeek();
curYear = mCalendar.get(Calendar.YEAR);
curMonth = mCalendar.get(Calendar.MONTH);
lastDay = getMonthLastDay(curYear, curMonth);
Log.d(TAG, "curYear : " + curYear + ", curMonth : " + curMonth + ", lastDay : " + lastDay);
int diff = mStartDay - Calendar.SUNDAY - 1;
startDay = getFirstDayOfWeek();
Log.d(TAG, "mStartDay : " + mStartDay + ", startDay : " + startDay);
}
// 이전달
public void setPreviousMonth() {
mCalendar.add(Calendar.MONTH, -1);
recalculate();
resetDayNumbers();
selectedPosition = -1;
}
// 다음달
public void setNextMonth() {
mCalendar.add(Calendar.MONTH, 1);
recalculate();
resetDayNumbers();
selectedPosition = -1;
}
public void resetDayNumbers() {
for (int i = 0; i < 42; i++) {
// calculate day number
int dayNumber = (i+1) - firstDay;
if (dayNumber < 1 || dayNumber > lastDay) {
dayNumber = 0;
}
// save as a data item
items[i] = new MonthItem(dayNumber);
}
}
private int getFirstDay(int dayOfWeek) {
int result = 0;
if (dayOfWeek == Calendar.SUNDAY) {
result = 0;
} else if (dayOfWeek == Calendar.MONDAY) {
result = 1;
} else if (dayOfWeek == Calendar.TUESDAY) {
result = 2;
} else if (dayOfWeek == Calendar.WEDNESDAY) {
result = 3;
} else if (dayOfWeek == Calendar.THURSDAY) {
result = 4;
} else if (dayOfWeek == Calendar.FRIDAY) {
result = 5;
} else if (dayOfWeek == Calendar.SATURDAY) {
result = 6;
}
return result;
}
public int getCurYear() {
return curYear;
}
public int getCurMonth() {
return curMonth;
}
public int getNumColumns() {
return 7;
}
public int getCount() {
return 7 * 6;
}
public Object getItem(int position) {
return items[position];
}
public long getItemId(int position) {
return position;
}
// 커스텀뷰 모양을 만들어줌
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "getView(" + position + ") called.");
MonthItemView itemView;
if (convertView == null) {
itemView = new MonthItemView(mContext);
} else {
itemView = (MonthItemView) convertView;
}
// create a params
GridView.LayoutParams params = new GridView.LayoutParams(
GridView.LayoutParams.MATCH_PARENT,
120);
// calculate row and column
int rowIndex = position / countColumn;
int columnIndex = position % countColumn;
Log.d(TAG, "Index : " + rowIndex + ", " + columnIndex);
// 그리드뷰 하나하나에 대한 ?
// set item data and properties
itemView.setItem(items[position]);
itemView.setLayoutParams(params);
itemView.setPadding(2, 2, 2, 2);
// set properties
itemView.setGravity(Gravity.LEFT);
if (columnIndex == 0) {
itemView.setTextColor(Color.RED);
}
else if (columnIndex == 6){
itemView.setTextColor(Color.BLUE);
}
else {
itemView.setTextColor(Color.BLACK);
}
// set background color
if (position == getSelectedPosition()) {
itemView.setBackgroundColor(Color.YELLOW);
} else {
itemView.setBackgroundColor(Color.WHITE);
}
return itemView;
}
/**
* Get first day of week as android.text.format.Time constant.
* @return the first day of week in android.text.format.Time
*/
public static int getFirstDayOfWeek() {
int startDay = Calendar.getInstance().getFirstDayOfWeek();
if (startDay == Calendar.SATURDAY) {
return Time.SATURDAY;
} else if (startDay == Calendar.MONDAY) {
return Time.MONDAY;
} else {
return Time.SUNDAY;
}
}
/**
* get day count for each month
*
* @param year
* @param month
* @return
*/
private int getMonthLastDay(int year, int month){
switch (month) {
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
return (31);
case 3:
case 5:
case 8:
case 10:
return (30);
default:
if(((year%4==0)&&(year%100!=0)) || (year%400==0) ) {
return (29); // 2월 윤년계산
} else {
return (28);
}
}
}
/**
* set selected row
*/
public void setSelectedPosition(int selectedPosition) {
this.selectedPosition = selectedPosition;
}
/**
* get selected row
*
* @return
*/
public int getSelectedPosition() {
return selectedPosition;
}
}
그리드뷰에 오늘날짜를 표시하려면 어떻게 바꿔야 할까요?
구글링해본 코드를 아무리 적용해봐도 바뀌지 않습니다...