有时候就是这样,研究一个问题,一开始想到了一个觉得可行的方案,然后去尝试;尝试了很久、很多次,已经要放弃了,关掉电脑心里
用ViewPager做左右滑动切换日期的效果;这次我遇到的是这样一个问题,由于系统的CalendarView不太美观,所以要自己实现一个日历的布局;所以想到了使用GridView,然后
package tk.sweetvvck.calender.adapter; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import tk.sweetvvck.calender.R; import android.app.Activity; import android.content.res.Resources; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class CalendarGridViewAdapter extends BaseAdapter { private Calendar calStartDate = Calendar.getInstance();// 当前显示的日历 private Calendar calToday = Calendar.getInstance(); // 今日 private int iMonthViewCurrentMonth = 0; // 当前视图月 // 根据改变的日期更新日历 // 填充日历控件用 private void UpdateStartDateForMonth() { calStartDate.set(Calendar.DATE, 1); // 设置成当月第一天 iMonthViewCurrentMonth = calStartDate.get(Calendar.MONTH);// 得到当前日历显示的月 // 星期一是2 星期天是1 填充剩余天数 int iDay = 0; int iFirstDayOfWeek = Calendar.MONDAY; int iStartDay = iFirstDayOfWeek; if (iStartDay == Calendar.MONDAY) { iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY; if (iDay < 0) iDay = 6; } if (iStartDay == Calendar.SUNDAY) { iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY; if (iDay < 0) iDay = 6; } calStartDate.add(Calendar.DAY_OF_WEEK, -iDay); calStartDate.add(Calendar.DAY_OF_MONTH, -1);// 周日第一位 } ArrayList<java.util.Date> titles; private ArrayList<java.util.Date> getDates() { UpdateStartDateForMonth(); ArrayList<java.util.Date> alArrayList = new ArrayList<java.util.Date>(); for (int i = 1; i <= 42; i++) { alArrayList.add(calStartDate.getTime()); calStartDate.add(Calendar.DAY_OF_MONTH, 1); } return alArrayList; } private Activity activity; Resources resources; // construct public CalendarGridViewAdapter(Activity a,Calendar cal) { calStartDate=cal; activity = a; resources=activity.getResources(); titles = getDates(); } public CalendarGridViewAdapter(Activity a) { activity = a; resources=activity.getResources(); } @Override public int getCount() { return titles.size(); } @Override public Object getItem(int position) { return titles.get(position); } @Override public long getItemId(int position) { return position; } @SuppressWarnings("deprecation") @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout iv = new LinearLayout(activity); iv.setGravity(Gravity.CENTER); iv.setOrientation(LinearLayout.VERTICAL); iv.setBackgroundColor(resources.getColor(R.color.white)); Date myDate = (Date) getItem(position); Calendar calCalendar = Calendar.getInstance(); calCalendar.setTime(myDate); final int iMonth = calCalendar.get(Calendar.MONTH); final int iDay = calCalendar.get(Calendar.DAY_OF_WEEK); // 判断周六周日 iv.setBackgroundColor(resources.getColor(R.color.white)); if (iDay == 7) { // 周六 iv.setBackgroundColor(resources.getColor(R.color.text_6)); } else if (iDay == 1) { // 周日 iv.setBackgroundColor(resources.getColor(R.color.text_7)); } else { } // 判断周六周日结束 TextView txtToDay = new TextView(activity); txtToDay.setGravity(Gravity.CENTER_HORIZONTAL); txtToDay.setTextSize(9); if (equalsDate(calToday.getTime(), myDate)) { // 当前日期 iv.setBackgroundColor(resources.getColor(R.color.selection)); txtToDay.setText("TODAY!"); } // 设置背景颜色结束 // 日期开始 TextView txtDay = new TextView(activity);// 日期 txtDay.setGravity(Gravity.CENTER_HORIZONTAL); // 判断是否是当前月 if (iMonth == iMonthViewCurrentMonth) { txtToDay.setTextColor(resources.getColor(R.color.ToDayText)); txtDay.setTextColor(resources.getColor(R.color.Text)); } else { txtDay.setTextColor(resources.getColor(R.color.noMonth)); txtToDay.setTextColor(resources.getColor(R.color.noMonth)); } int day = myDate.getDate(); // 日期 txtDay.setText(String.valueOf(day)); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); iv.addView(txtDay, lp); LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); iv.addView(txtToDay, lp1); return iv; } @SuppressWarnings("deprecation") private Boolean equalsDate(Date date1, Date date2) { if (date1.getYear() == date2.getYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate()) { return true; } else { return false; } } }
/* * Copyright 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package tk.sweetvvck.calender.activity; import java.util.Calendar; import tk.sweetvvck.calender.R; import tk.sweetvvck.calender.adapter.CalendarGridViewAdapter; import tk.sweetvvck.calender.utils.Utils; import android.app.Activity; import android.app.Fragment; import android.content.res.Resources; import android.graphics.Color; import android.os.Bundle; import android.view.Display; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class CalendarFragment extends Fragment { public static final String ARG_PAGE = "page"; private int mPageNumber; private Calendar mCalendar; private CalendarGridViewAdapter calendarGridViewAdapter; public static Fragment create(int pageNumber) { CalendarFragment fragment = new CalendarFragment(); Bundle args = new Bundle(); args.putInt(ARG_PAGE, pageNumber); fragment.setArguments(args); return fragment; } public CalendarFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPageNumber = getArguments().getInt(ARG_PAGE); mCalendar = Utils.getSelectCalendar(mPageNumber); calendarGridViewAdapter = new CalendarGridViewAdapter(getActivity(), mCalendar); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout containing a title and body text. ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.calendar_view, container, false); GridView titleGridView = (GridView) rootView .findViewById(R.id.gridview); TitleGridAdapter titleAdapter = new TitleGridAdapter(getActivity()); initGridView(titleGridView, titleAdapter); GridView calendarView = (GridView) rootView .findViewById(R.id.calendarView); initGridView(calendarView, calendarGridViewAdapter); calendarView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { for (int i = 0; i < parent.getCount(); i++) { if ((i % 7) == 6) { parent.getChildAt(i).setBackgroundColor( getActivity().getResources().getColor( R.color.text_6)); } else if ((i % 7) == 0) { parent.getChildAt(i).setBackgroundColor( getActivity().getResources().getColor( R.color.text_7)); } else { parent.getChildAt(i).setBackgroundColor( Color.TRANSPARENT); } } view.setBackgroundColor(getActivity().getResources().getColor( R.color.selection)); } }); return rootView; } private void initGridView(GridView gridView, BaseAdapter adapter) { gridView = setGirdView(gridView); gridView.setAdapter(adapter);// 设置菜单Adapter } @SuppressWarnings("deprecation") private GridView setGirdView(GridView gridView) { gridView.setNumColumns(7);// 设置每行列数 gridView.setGravity(Gravity.CENTER_VERTICAL);// 位置居中 gridView.setVerticalSpacing(1);// 垂直间隔 gridView.setHorizontalSpacing(1);// 水平间隔 gridView.setBackgroundColor(getResources().getColor( R.color.calendar_background)); WindowManager windowManager = getActivity().getWindowManager(); Display display = windowManager.getDefaultDisplay(); int i = display.getWidth() / 7; int j = display.getWidth() - (i * 7); int x = j / 2; gridView.setPadding(x, 0, 0, 0);// 居中 return gridView; } public class TitleGridAdapter extends BaseAdapter { int[] titles = new int[] { R.string.Sun, R.string.Mon, R.string.Tue, R.string.Wed, R.string.Thu, R.string.Fri, R.string.Sat }; private Activity activity; // construct public TitleGridAdapter(Activity a) { activity = a; } @Override public int getCount() { return titles.length; } @Override public Object getItem(int position) { return titles[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout iv = new LinearLayout(activity); TextView txtDay = new TextView(activity); txtDay.setFocusable(false); txtDay.setBackgroundColor(Color.TRANSPARENT); iv.setOrientation(LinearLayout.VERTICAL); txtDay.setGravity(Gravity.CENTER); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); int i = (Integer) getItem(position); txtDay.setTextColor(Color.GRAY); Resources res = getResources(); if (i == R.string.Sat) { // 周六 txtDay.setBackgroundColor(res.getColor(R.color.title_text_6)); } else if (i == R.string.Sun) { // 周日 txtDay.setBackgroundColor(res.getColor(R.color.title_text_7)); } else { } txtDay.setText((Integer) getItem(position)); iv.addView(txtDay, lp); return iv; } } }
package tk.sweetvvck.calender.activity; import java.util.Calendar; import tk.sweetvvck.calender.R; import tk.sweetvvck.calender.utils.Utils; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.support.v13.app.FragmentStatePagerAdapter; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.widget.TextView; /** * 日历 * @author 程科 */ public class MainActivity extends FragmentActivity { private ViewPager viewPager; private TextView tvMonth; private String month; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) this.findViewById(R.id.viewpager); final ScreenSlidePagerAdapter screenSlidePagerAdapter = new ScreenSlidePagerAdapter( getFragmentManager()); viewPager.setAdapter(screenSlidePagerAdapter); viewPager.setCurrentItem(500); tvMonth = (TextView) this.findViewById(R.id.tv_month); month = Calendar.getInstance().get(Calendar.YEAR) + "-" + Utils.LeftPad_Tow_Zero(Calendar.getInstance().get( Calendar.MONTH) + 1); tvMonth.setText(month); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { Calendar calendar = Utils.getSelectCalendar(position); month = calendar.get(Calendar.YEAR) + "-" + Utils.LeftPad_Tow_Zero(calendar.get(Calendar.MONTH) + 1); tvMonth.setText(month); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); } private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return CalendarFragment.create(position); } @Override public int getCount() { return 1000; } } }