๐ปTech/๐ตandroid
[์๋๋ก์ด๋] ๊ฐ๋จํ๊ณ ์ฌ์ด ๋ฌ๋ ฅ ์์ค(GridView)
_viper_
2015. 9. 15. 11:00
๋ฐ์ํ
GridView๋ก ๊ฐ๋จํ ๋ฌ๋ ฅ์ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
1. ๋ ์ด์์ ์์ฑ
-ํ์ฌ ์ฐ/์์ ๋ณด์ฌ์ค ํ
์คํธ๋ทฐ์ Calendar๋ฅผ ์์ฑํ ๊ทธ๋ฆฌ๋๋ทฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
res->layout
activity_calendar.xml (@color/color_ffffff๋ฅผ ์ฌ์ฉํ์๋ ค๋ฉด color.xml์ ์์ ํด์ฃผ์ธ์.)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/color_ffffff" >
<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/color_21a4ff"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="@color/color_ffffff"
android:textSize="20sp" />
<GridView
android:layout_marginTop="10dp"
android:id="@+id/gridview"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent"
android:numColumns="7"
android:listSelector="@color/transparent"
>
</GridView>
</LinearLayout>
item_calendar_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:gravity="center_horizontal"
android:id="@+id/tv_item_gridview"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="20sp"
/>
</LinearLayout>
2. ์๋ฐ์ฝ๋ ์์ฑ
CalendarActivity.java
package com.example.calendar;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class CalendarActivity extends Activity {
/**
* ์ฐ/์ ํ
์คํธ๋ทฐ
*/
private TextView tvDate;
/**
* ๊ทธ๋ฆฌ๋๋ทฐ ์ด๋ํฐ
*/
private GridAdapter gridAdapter;
/**
* ์ผ ์ ์ฅ ํ ๋ฆฌ์คํธ
*/
private ArrayList<String> dayList;
/**
* ๊ทธ๋ฆฌ๋๋ทฐ
*/
private GridView gridView;
/**
* ์บ๋ฆฐ๋ ๋ณ์
*/
private Calendar mCal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
tvDate = (TextView)findViewById(R.id.tv_date);
gridView = (GridView)findViewById(R.id.gridview);
// ์ค๋์ ๋ ์ง๋ฅผ ์ธํ
ํด์ค๋ค.
long now = System.currentTimeMillis();
final Date date = new Date(now);
//์ฐ,์,์ผ์ ๋ฐ๋ก ์ ์ฅ
final SimpleDateFormat curYearFormat = new SimpleDateFormat("yyyy", Locale.KOREA);
final SimpleDateFormat curMonthFormat = new SimpleDateFormat("MM", Locale.KOREA);
final SimpleDateFormat curDayFormat = new SimpleDateFormat("dd", Locale.KOREA);
//ํ์ฌ ๋ ์ง ํ
์คํธ๋ทฐ์ ๋ฟ๋ ค์ค
tvDate.setText(curYearFormat.format(date) + "/" + curMonthFormat.format(date));
//gridview ์์ผ ํ์
dayList = new ArrayList<String>();
dayList.add("์ผ");
dayList.add("์");
dayList.add("ํ");
dayList.add("์");
dayList.add("๋ชฉ");
dayList.add("๊ธ");
dayList.add("ํ ");
mCal = Calendar.getInstance();
//์ด๋ฒ๋ฌ 1์ผ ๋ฌด์จ์์ผ์ธ์ง ํ๋จ mCal.set(Year,Month,Day)
mCal.set(Integer.parseInt(curYearFormat.format(date)), Integer.parseInt(curMonthFormat.format(date)) - 1, 1);
int dayNum = mCal.get(Calendar.DAY_OF_WEEK);
//1์ผ - ์์ผ ๋งค์นญ ์ํค๊ธฐ ์ํด ๊ณต๋ฐฑ add
for (int i = 1; i < dayNum; i++) {
dayList.add("");
}
setCalendarDate(mCal.get(Calendar.MONTH) + 1);
gridAdapter = new GridAdapter(getApplicationContext(), dayList);
gridView.setAdapter(gridAdapter);
}
/**
* ํด๋น ์์ ํ์ํ ์ผ ์ ๊ตฌํจ
*
* @param month
*/
private void setCalendarDate(int month) {
mCal.set(Calendar.MONTH, month - 1);
for (int i = 0; i < mCal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
dayList.add("" + (i + 1));
}
}
/**
* ๊ทธ๋ฆฌ๋๋ทฐ ์ด๋ํฐ
*
*/
private class GridAdapter extends BaseAdapter {
private final List<String> list;
private final LayoutInflater inflater;
/**
* ์์ฑ์
*
* @param context
* @param list
*/
public GridAdapter(Context context, List<String> list) {
this.list = list;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public String getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_calendar_gridview, parent, false);
holder = new ViewHolder();
holder.tvItemGridView = (TextView)convertView.findViewById(R.id.tv_item_gridview);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.tvItemGridView.setText("" + getItem(position));
//ํด๋น ๋ ์ง ํ
์คํธ ์ปฌ๋ฌ,๋ฐฐ๊ฒฝ ๋ณ๊ฒฝ
mCal = Calendar.getInstance();
//์ค๋ day ๊ฐ์ ธ์ด
Integer today = mCal.get(Calendar.DAY_OF_MONTH);
String sToday = String.valueOf(today);
if (sToday.equals(getItem(position))) { //์ค๋ day ํ
์คํธ ์ปฌ๋ฌ ๋ณ๊ฒฝ
holder.tvItemGridView.setTextColor(getResources().getColor(R.color.color_000000));
}
return convertView;
}
}
private class ViewHolder {
TextView tvItemGridView;
}
}
[๊ฒฐ๊ณผ ํ๋ฉด]