找传奇、传世资源到传世资源站!

android_日历例子源码下载_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

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;
  }
}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复