android 日历的实现
package com.calendarProject.Activities; import java.util.Calendar; import com.calendarProject.Activities.R; import com.calendarProject.Adapters.CalendarGridAdapter; import com.calendarProject.Adapters.TitleGridAdapter; import com.calendarProject.MyGridView.MyGridView; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Fragment; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.Toast; @SuppressLint("NewApi") public class CalendarFragment extends Fragment{ private int startMonth; private int position; private Calendar currentDate; private int[] solarData; private String[] lunarData; private TitleGridAdapter tAdapter; private CalendarGridAdapter cAdapter; private MyGridView titleGrid; private GridView calendarGrid; //定义一个静态方法 public static Fragment createCalendar(int position, int startMonth, int[] gridData, String[] lunarData) { CalendarFragment view = new CalendarFragment(); Bundle args = new Bundle(); args.putCharSequenceArray("lunarData", lunarData); args.putIntArray("solarData", gridData); args.putInt("position", position); args.putInt("startMonth", startMonth); view.setArguments(args); Log.i("tag1", "create Fragement" position); return view; } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); position = getArguments().getInt("position"); startMonth = getArguments().getInt("startMonth"); solarData = getArguments().getIntArray("solarData"); lunarData = (String[]) getArguments().getCharSequenceArray("lunarData"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub System.out.println("createView"); //将currentDate设为当前的日期 currentDate = Calendar.getInstance(); currentDate.add(Calendar.MONTH, position - startMonth); //获取fragment_calendar布局 ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_calendar, container, false); LinearLayout ll = (LinearLayout)rootView.findViewById(R.id.LinearLayout); //创建TitleGrid组件 titleGrid = new MyGridView(getActivity()); //创建TitleGrid组件 calendarGrid = new MyGridView(getActivity()); //实例化两个gridview的适配器 tAdapter = new TitleGridAdapter(getActivity()); cAdapter = new CalendarGridAdapter(getActivity(), currentDate, solarData, lunarData); //设置gridview的一些属性 titleGrid.setNumColumns(7); calendarGrid.setNumColumns(7); titleGrid.setBackgroundResource(R.drawable.titlegrid_bg); calendarGrid.setBackgroundResource(R.drawable.calendargrid_bg); titleGrid.setGravity(Gravity.CENTER_VERTICAL); calendarGrid.setGravity(Gravity.CENTER); calendarGrid.setVerticalSpacing(20); titleGrid.setSelector(R.drawable.gridselector); calendarGrid.setSelector(R.drawable.gridselector); //加载适配器 titleGrid.setAdapter(tAdapter); calendarGrid.setAdapter(cAdapter); //clanedarGrid加载监听器 calendarGrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if(solarData[arg2] != 0) Toast.makeText(getActivity(), arg2 "", Toast.LENGTH_SHORT).show();; } }); //将gridView加载至viewgroup即linearLayout ll.addView(titleGrid); ll.addView(calendarGrid, new LayoutParams(LayoutParams.WRAP_CONTENT, 1300)); return rootView; } }
评论