Android Open Source - JKCalendar Calendar Adapter






From Project

Back to project page JKCalendar.

License

The source code is released under:

Apache License

If you think the Android project JKCalendar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jakevin.com.jkcalendar_lib.ui.adapter;
//from  w  ww. j av a 2 s .  c  om
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import jakevin.com.jkcalendar_lib.R;
import jakevin.com.jkcalendar_lib.ui.JKCalendar;

/**
 * Gallery????Adapter
 *
 * Created by jakevin on 14/11/20.
 */
public class CalendarAdapter extends BaseAdapter {

    protected LayoutInflater layoutInflater;
    Context mContext;

    ArrayList<Boolean> clicks;

    ArrayList<Calendar> calendars;

    int color_high = Color.BLUE;
    int color_hide = Color.GRAY;

    public CalendarAdapter(Context context, ArrayList<Calendar> calendars){
        this.layoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.calendars = calendars;

        clicks = new ArrayList<Boolean>();
        for(int i = 0 ; i < calendars.size() ; i++){
            clicks.add(false);
        }

        color_hide = mContext.getResources().getColor(android.R.color.darker_gray);
        color_high = mContext.getResources().getColor(android.R.color.holo_blue_light);
    }

    public void setColor(int high , int hide){
        color_high = high;
        color_hide = hide;
    }

    private class ViewHodler{
        TextView dayOfMonth;
        TextView dayOfWeek;
    }

    public void setClickIndex(int position){
        for(int i = 0 ; i < clicks.size() ; i++){
            clicks.set(i,false);
        }
        if(position>=0){
            clicks.set(position,true);
        }
    }

    @Override
    public int getCount() {
        return calendars.size();
    }

    @Override
    public Calendar getItem(int i) {
        return calendars.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHodler viewHodler;

        if (convertView == null) {
//            convertView = layoutInflater.inflate(JKCalendar.getResourseIdByName(mContext.getPackageName(),"layout","calender_adpter_layout"), null);
//
//            viewHodler = new ViewHodler();
//
//            viewHodler.dayOfMonth = (TextView) convertView.findViewById(JKCalendar.getResourseIdByName(mContext.getPackageName(),"id","day_of_month"));
//
//            viewHodler.dayOfWeek = (TextView) convertView.findViewById(JKCalendar.getResourseIdByName(mContext.getPackageName(),"id","day_of_week"));

            convertView = layoutInflater.inflate(R.layout.calender_adpter_layout, null);

            viewHodler = new ViewHodler();

            viewHodler.dayOfMonth = (TextView) convertView.findViewById(R.id.day_of_month);

            viewHodler.dayOfWeek = (TextView) convertView.findViewById(R.id.day_of_week);


            convertView.setTag(viewHodler);
        }

        viewHodler = (ViewHodler) convertView.getTag();

        convertView.setLayoutParams(new Gallery.LayoutParams(convertDipToPixels(mContext,50), convertDipToPixels(mContext,60)));

        Calendar tempCalendar = calendars.get(position);

        viewHodler.dayOfMonth.setText(String.format("%02d",tempCalendar.get(Calendar.DAY_OF_MONTH)));

        viewHodler.dayOfMonth.setTag(tempCalendar);

        SimpleDateFormat parserSDF = new SimpleDateFormat("EEE", Locale.ENGLISH);

        viewHodler.dayOfWeek.setText(parserSDF.format(tempCalendar.getTime()));

        if(clicks.get(position)){
            viewHodler.dayOfWeek.setTextColor(color_high);
            viewHodler.dayOfMonth.setTextColor(color_high);
        }else{
            viewHodler.dayOfWeek.setTextColor(color_hide);
            viewHodler.dayOfMonth.setTextColor(color_hide);
        }

        return convertView;
    }

    public static int convertDipToPixels(Context context, float dips) {
        return (int) (dips * context.getResources().getDisplayMetrics().density + 0.5f);
    }
}




Java Source Code List

jakevin.com.jkcalendar.ApplicationTest.java
jakevin.com.jkcalendar.MainActivity.java
jakevin.com.jkcalendar_lib.ApplicationTest.java
jakevin.com.jkcalendar_lib.ui.JKCalendar.java
jakevin.com.jkcalendar_lib.ui.adapter.CalendarAdapter.java
jakevin.com.jkcalendar_lib.ui.adapter.CalendarConfig.java
jakevin.com.jkcalendar_lib.ui.adapter.GridCellAdapter.java