源码简介:自己改造的实用的日历控件,pop效果,附带动画。
下载地址:http://down.51cto.com/data/2194851
源码效果:
源码片段:
- public static synchronized PopCalendar getInstance(Context context, View parent, int gravity, OnCellClickListener onClickdate,
- OnClickListener onConfirm, OnDismissListener onDismiss) {
- if (gravity != -1) {
- GRAVITY_FOR_POP = gravity;
- }
- if (context == null) {
- return null;
- }
- if (parent == null) {
- return null;
- }
- if (null == instance) {
- View view = LayoutInflater.from(context).inflate(R.layout.layout_calendar, null);
- mViewPager = (ViewPager) view.findViewById(R.id.activity_user_calendar_Viewpager);
- preImgBtn = (ImageButton) view.findViewById(R.id.activity_user_calendar_PreMonthBtn);
- nextImgBtn = (ImageButton) view.findViewById(R.id.activity_user_calendar_NextMonthBtn);
- slide_time = (TextView) view.findViewById(R.id.slide_time);
- TextView btn_confirm = (TextView) view.findViewById(R.id.tv_confirm);
- // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
- instance = new PopCalendar(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, parent);
- final OnCellClickListener listener = onClickdate;
- preImgBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
- }
- });
- nextImgBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
- }
- });
- Calendar c = Calendar.getInstance();
- CalendarCard[] views = new CalendarCard[3];
- for (int i = 0; i < 3; i++) {
- views[i] = new CalendarCard(context, new OnCellClickListener() {
- @Override
- public void clickDate(CustomDate date) {
- if (listener != null) {
- listener.clickDate(date);
- }
- // window.dismiss();
- }
- @Override
- public void changeDate(CustomDate date) {
- slide_time.setText(date.year + "年" + date.month + "月");
- }
- }, liang);
- }
- adapter = new CalendarViewAdapter<calendarcard>(views);
- setViewPager();
- // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
- instance.setFocusable(true);
- // 实例化一个ColorDrawable颜色为半透明
- ColorDrawable dw = new ColorDrawable(0xffffffff);
- instance.setBackgroundDrawable(dw);
- final OnClickListener onclick = onConfirm;
- btn_confirm.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (onclick != null) {
- onclick.onClick(v);
- }
- instance.dismiss();
- }
- });
- // 设置popWindow的显示和消失动画
- instance.setAnimationStyle(R.style.mypopwindow_anim_style);
- // 在底部显示
- // View parent = activity.findViewById(R.id.contact_main);
- // window.showAtLocation(parent, GRAVITY_FOR_POP, X_LOCATION,
- // Y_LOCATION);
- final OnDismissListener dListener = onDismiss;
- // popWindow消失监听方法
- instance.setOnDismissListener(new OnDismissListener() {
- @Override
- public void onDismiss() {
- if (dListener != null) {
- dListener.onDismiss();
- }
- System.out.println("popWindow消失");
- }
- });
- }
- return instance;
- }</calendarcard>