Android日历周视图 可添加事件标记

                         Android日历周视图 可添加事件标记_第1张图片                                    Android日历周视图 可添加事件标记_第2张图片      


package cn.edu.sxu.www.customercalendar;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private CalendarAdapger calendarAdapger;
    private List lists;

    private TextView tv_tip;



    private float lastValue;
    private boolean isRightToLeft = false;  //从右向左滑动
    private boolean isLeftToRight = false;  //从左向右滑动

    private String currentDate;

    private DayItem oldDayItem;

    private List eventList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        eventList=new ArrayList<>();
        eventList.add("20170308");//实际上是四月八号的事件
        eventList.add("20170303");
        eventList.add("20161222");
        eventList.add("20170403");
        eventList.add("20170508");
        eventList.add("20170512");

        viewPager= (ViewPager) findViewById(R.id.viewpater);
        tv_tip= (TextView) findViewById(R.id.tv_tip);
        lists=new ArrayList<>();
        Calendar calendar= Calendar.getInstance();
        int day=calendar.get(Calendar.DAY_OF_MONTH);
        int week=calendar.get(Calendar.DAY_OF_WEEK);
        int month=calendar.get(Calendar.MONTH);
        int year=calendar.get(Calendar.YEAR);
        currentDate=""+year+(month<10?"0"+month:month+"")+(day<10?"0"+day:day+"");
        int lastDay=getLastDay(calendar);
        String[] nums=makeViewpagerItem(day,week,lastDay,-1,month,year);

        tv_tip.setText(year+"年"+(month+1)+"月");


        //计算下一页数据
        if(TextUtils.isEmpty(nums[1])||Integer.parseInt(nums[1])==lastDay)
        {
            //下一月份
            //判断是不是该到下一年
            if(month==11)
            {
                //取下一年
                calendar.set(year+1,0,1);
            }
            else
            {
                calendar.set(year,month+1,1);


            }
        }
        else
        {
            //本月
            calendar.set(year,month,Integer.parseInt(nums[1])+1);

        }
        int day1=calendar.get(Calendar.DAY_OF_MONTH);
        int week1=calendar.get(Calendar.DAY_OF_WEEK);
        int month1=calendar.get(Calendar.MONTH);
        int year1=calendar.get(Calendar.YEAR);
        int lastDay1=getLastDay(calendar);
        String[] nums1=makeViewpagerItem(day1,week1,lastDay1,-1,month1,year1);

        //计算前一页数据
        if(TextUtils.isEmpty(nums[0])||Integer.parseInt(nums[0])==0)
        {
            //前一月份
            //判断是不是该到前一年
            if(month==0)
            {
                //取上一年
                calendar.set(year-1,11,1);
                int week2=calendar.get(Calendar.DAY_OF_WEEK);
                int lastDay2=getLastDay(calendar);
                int date=lastDay2-(week2-1);
                calendar.set(year-1,11,date);
            }
            else
            {
                calendar.set(Calendar.MONTH,month-1);
                int lastDay2=getLastDay(calendar);
                int week2=calendar.get(Calendar.DAY_OF_WEEK);
                int  date=lastDay2-(week2-1);
                calendar.set(year,month-1,date);

            }
        }
        else
        {
            //本月
            calendar.set(year,month,Integer.parseInt(nums[0])-1);

        }
        day=calendar.get(Calendar.DAY_OF_MONTH);
        week=calendar.get(Calendar.DAY_OF_WEEK);
        month=calendar.get(Calendar.MONTH);
        year=calendar.get(Calendar.YEAR);
        lastDay=getLastDay(calendar);
        makeViewpagerItem(day,week,lastDay,0,month,year);



        calendarAdapger=new CalendarAdapger(lists,this);
        viewPager.setAdapter(calendarAdapger);
        viewPager.setCurrentItem(Integer.MAX_VALUE/2+1);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (positionOffset == 0.0){
                    return;
                }
                if (lastValue > positionOffset) {
                    // 递减,从左向右滑动
                    isRightToLeft = false;
                    isLeftToRight = true;
                } else if (lastValue < positionOffset) {
                    // 递增,从右向左滑动
                    isRightToLeft = true;
                    isLeftToRight = false;
                } else if (lastValue == positionOffset) {
                    isLeftToRight = isRightToLeft = false;
                }
                lastValue = positionOffset;

            }

            @Override
            public void onPageSelected(int position) {
                viewPagerItem viewPagerItem=lists.get(position%3);
                tv_tip.setText(viewPagerItem.getYear()+"年"+(viewPagerItem.getMonth()+1)+"月");

                if(isRightToLeft)//右边滑动
                {
                    String[] nums=viewPagerItem.getNums();
                    int month=viewPagerItem.getMonth();
                    int year=viewPagerItem.getYear();
                    int lastDay=viewPagerItem.getLastDay();
                    Calendar calendar=Calendar.getInstance();
                    //计算下一页数据
                    if(TextUtils.isEmpty(nums[1])||Integer.parseInt(nums[1])==lastDay)
                    {
                        //下一月份
                        //判断是不是该到下一年
                        if(month==11)
                        {
                            //取下一年
                            calendar.set(year+1,0,1);
                        }
                        else
                        {
                            calendar.set(year,month+1,1);
                        }
                    }
                    else
                    {
                        //本月
                        calendar.set(year,month,Integer.parseInt(nums[1])+1);

                    }
                    int day1=calendar.get(Calendar.DAY_OF_MONTH);
                    int week1=calendar.get(Calendar.DAY_OF_WEEK);
                    int month1=calendar.get(Calendar.MONTH);
                    int year1=calendar.get(Calendar.YEAR);
                    int lastDay1=getLastDay(calendar);
                    makeViewpagerItem(day1,week1,lastDay1,month1,year1,lists.get((position-2)%3));
                    calendarAdapger.notifyDataSetChanged();

                }
                else//左边滑动
                {
                    String[] nums=viewPagerItem.getNums();
                    int month=viewPagerItem.getMonth();
                    int year=viewPagerItem.getYear();
                    Calendar calendar=Calendar.getInstance();
                    //计算前一页数据
                    if(TextUtils.isEmpty(nums[0])||Integer.parseInt(nums[0])==0)
                    {
                        //前一月份
                        //判断是不是该到前一年
                        if(month==0)
                        {
                            //取上一年


                            calendar.set(year-1,11,1);
                            int week=calendar.get(Calendar.DAY_OF_WEEK);
                            int lastDay=getLastDay(calendar);
                            int date=lastDay-(week-1);
                            calendar.set(year-1,11,date);
                        }
                        else
                        {
                            calendar.set(Calendar.MONTH,month-1);
                            int lastDay=getLastDay(calendar);
                            int week=calendar.get(Calendar.DAY_OF_WEEK);
                            int  date=lastDay-(week-1);
                            calendar.set(year,month-1,date);

                        }
                    }
                    else
                    {
                        //本月
                        calendar.set(year,month,Integer.parseInt(nums[0])-1);

                    }
                    int  day1=calendar.get(Calendar.DAY_OF_MONTH);
                    int  week1=calendar.get(Calendar.DAY_OF_WEEK);
                    int month1=calendar.get(Calendar.MONTH);
                    int year1=calendar.get(Calendar.YEAR);
                    int lastDay1=getLastDay(calendar);
                    makeViewpagerItem(day1,week1,lastDay1,month1,year1,lists.get((position+2)%3));
                    calendarAdapger.notifyDataSetChanged();
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



    }


    public DayItem getOldDayItem() {
        return oldDayItem;
    }

    public void setOldDayItem(DayItem oldDayItem) {
        this.oldDayItem = oldDayItem;
    }

    /**
     * 返回第一个数和最后一个数
     * @param parameterday
     * @param week
     * @param lastDay
     * @return
     */
    private String[] makeViewpagerItem(int parameterday,int week,int lastDay,int flag,int month,int year)
    {
        int day=parameterday;
        List dateBeens=new ArrayList<>();
        for(int i=week;i>0;i--)
        {
            if(day<1)
            {
                dateBeens.add(0,new DateBean("",false,month<10?"0"+month:month+"",year+""));
            }
            else
            {
                dateBeens.add(0,new DateBean(day<10?"0"+day:day+"",false,month<10?"0"+month:month+"",year+""));
            }
            day--;
        }

        day=parameterday;
        for(int i=week+1;i<=7;i++)
        {
            day++;
            if(day>lastDay)
            {
                dateBeens.add(new DateBean("",false,month<10?"0"+month:month+"",year+""));
            }
            else
            {
                dateBeens.add(new DateBean(day<10?"0"+day:day+"",false,month<10?"0"+month:month+"",year+""));
            }
        }
        //给"今天"做标识
        for(int i=0;i dateBeens=new ArrayList<>();
        for(int i=week;i>0;i--)
        {
            if(day<1)
            {
                dateBeens.add(0,new DateBean("",false,month<10?"0"+month:month+"",year+""));
            }
            else
            {
                dateBeens.add(0,new DateBean(day<10?"0"+day:day+"",false,month<10?"0"+month:month+"",year+""));
            }
            day--;
        }

        day=parameterday;
        for(int i=week+1;i<=7;i++)
        {
            day++;
            if(day>lastDay)
            {
                dateBeens.add(new DateBean("",false,month<10?"0"+month:month+"",year+""));
            }
            else
            {
                dateBeens.add(new DateBean(day<10?"0"+day:day+"",false,month<10?"0"+month:month+"",year+""));
            }
        }
        String [] num=new String[2];
        num[0]=dateBeens.get(0).getDate();
        num[1]=dateBeens.get(6).getDate();
        viewPagerItem.setDay(parameterday);
        viewPagerItem.setMonth(month);
        viewPagerItem.setYear(year);
        viewPagerItem.setNums(num);

        viewPagerItem.setData(dateBeens);
        return num;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(String currentDate) {
        this.currentDate = currentDate;
    }

    public List getEventList() {
        return eventList;
    }

    public void setEventList(List eventList) {
        this.eventList = eventList;
    }
}



package cn.edu.sxu.www.customercalendar;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by ${huozhenpeng} on 17/3/27.
 * Company : www.miduo.com
 */

public class CalendarAdapger extends PagerAdapter {


    private List lists;
    private Context context;
    public CalendarAdapger(List lists,Context context)
    {

        this.lists=lists;
        this.context=context;
    }
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }



    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Log.e("abc","destroyItem----->"+(position%3));
//        container.removeView(lists.get(position%3));
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.e("abc","instantiateItem------>"+(position%3));

        viewPagerItem viewPagerItem=lists.get(position%3);
        String log="";

        for(int i=0;i



package cn.edu.sxu.www.customercalendar;

import android.text.TextUtils;

import java.io.Serializable;

/**
 * Created by ${huozhenpeng} on 17/3/27.
 * Company : www.miduo.com
 */

public class DateBean implements Serializable {

    private String  date;
    private boolean needFlag;
    private boolean isToday;

    private String month;

    private String year;

    private String strDate;

    public String getStrDate() {
        strDate = year + month + date;
        return strDate;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public boolean isToday() {
        return isToday;
    }

    public void setToday(boolean today) {
        isToday = today;
    }



    public DateBean(String date, boolean needFlag,String month,String year)
    {
        this.date=date;
        this.needFlag=needFlag;
        this.month=month;
        this.year=year;
    }
    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public boolean isNeedFlag() {
        return needFlag;
    }

    public void setNeedFlag(boolean needFlag) {
        this.needFlag = needFlag;
    }


}



package cn.edu.sxu.www.customercalendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by ${huozhenpeng} on 17/3/27.
 * Company : www.miduo.com
 */

public class DayItem extends View {

    private int width;
    private int height;
    private TextPaint textPaint;
    private Paint pointPaint;

    private Paint circlePaint;
    private int radius;

    private int circleWidth;
    private MainActivity context;
    public DayItem(Context context) {
        this(context,null);
    }

    public DayItem(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public DayItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context= (MainActivity) context;
        init();
    }

    private void init()
    {
        textPaint=new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.parseColor("#000000"));
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(context.getResources().getDimension(R.dimen.px2sp_24));
        pointPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        pointPaint.setColor(Color.parseColor("#369ed2"));
        radius= (int) context.getResources().getDimension(R.dimen.px2dp_6);
        pointPaint.setStrokeWidth(radius);

        circlePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.parseColor("#ff0000"));
        circleWidth= (int) context.getResources().getDimension(R.dimen.px2dp_2);
        circlePaint.setStrokeWidth(circleWidth);
        circlePaint.setStyle(Paint.Style.STROKE);

    }

    private int baseY;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //绘制文字
        // 计算Baseline绘制的Y坐标
        baseY = (int) ((height/ 2) - ((textPaint.descent() + textPaint.ascent()) / 2));

        canvas.drawText(dateBean.getDate(), width/2, baseY, textPaint);

        //绘制标记

        if(dateBean.isNeedFlag()) {
            canvas.drawCircle(width / 2, height - radius, radius, pointPaint);
        }

        //绘制红色圆环
        if(dateBean.isToday())
        {
            if(width>height)
            {
                canvas.drawCircle(width/2,height/2,height/2-circleWidth/2,circlePaint);
            }
            else
            {
                canvas.drawCircle(width/2,height/2,width/2-circleWidth/2,circlePaint);
            }

        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width=w;
        height=h;
    }

    private DateBean dateBean;
    public  void setData(DateBean dateBean)
    {
        this.dateBean=dateBean;
    }


    public DateBean getData()
    {
        return this.dateBean;
    }

}


package cn.edu.sxu.www.customercalendar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.List;

/**
 * Created by ${huozhenpeng} on 17/3/27.
 * Company : www.miduo.com
 */

public class viewPagerItem extends LinearLayout implements View.OnClickListener{
    private MainActivity context;
    private List lists;
    private int day;
    private int week;
    private int month;
    private int year;
    private int lastDay;
    private String[] nums;

    private List events;




    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getWeek() {
        return week;
    }

    public void setWeek(int week) {
        this.week = week;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getLastDay() {
        return lastDay;
    }

    public void setLastDay(int lastDay) {
        this.lastDay = lastDay;
    }

    public String[] getNums() {
        return nums;
    }

    public void setNums(String[] nums) {
        this.nums = nums;
    }

    public viewPagerItem(Context context) {
        this(context,null);
    }

    public viewPagerItem(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public viewPagerItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context= (MainActivity) context;
    }
    public void setData(List lists)
    {
        this.lists=lists;
        removeAllViews();
        for(int i=0;i







    
    

    


    







    

    

    

    

    

    

    



    CalendarWeek
    
    
    
    
    
    
    





    #3F51B5
    #303F9F
    #FF4081
    #cdcdcd
    #ffffffff





源码下载:

点击打开链接

你可能感兴趣的:(Android自定义控件)