๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ปTech/๐ŸŒตandroid

[์•ˆ๋“œ๋กœ์ด๋“œ] ๊ฐ„๋‹จํ•˜๊ณ  ์‰ฌ์šด ๋‹ฌ๋ ฅ ์†Œ์Šค(GridView)

by _viper_ 2015. 9. 15.
๋ฐ˜์‘ํ˜•

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;
    }

}

 

[๊ฒฐ๊ณผ ํ™”๋ฉด]