ViewPager+GridView 日历手势滑动

思路:使用GridView进行日历数据的存储,使用ViewPager进行日历页面的切换。当然也可以使用GestureDetector+ViewFlipper进行页面的的手势控制和页面切换。首先写好GridView的adapter,用数组存储三个Gridview表示这上一个月,这一个月,和下一个月,并且添加item的点击事件,表示选中的日期。然后将这三个Gridview作为viewpager的item,并且给Viewpager添加页面改变的监听,当页面改变的时候,数据要跟着改变。注意:viewpager本身是不能进行循环显示的,因此要自己想办法 可以看我的另一篇博客。https://mp.csdn.net/postedit/79963491


MainActivity

        public class MainActivity extends Activity implements OnClickListener{
	 private List<MyGridView> mLists;  
	 private int index = 1;
	 private MyPagerListener listener;
	 
	 private ViewPager viewPager;
	 private MyViewPagerAdapter pagerAdapter;
	 
	 private TextView diary_title;
	 private Button lastButton;
	 private Button nextButton;
	 private CalendarAdapter gridAdapter;
	 
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);	
		init();	
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		diary_title = (TextView) findViewById(R.id.diary_title);
		lastButton = (Button) findViewById(R.id.lastButton);
		nextButton = (Button) findViewById(R.id.nextButton);
		pagerAdapter = new MyViewPagerAdapter(this, mLists);
		viewPager.setAdapter(pagerAdapter);
		listener = new MyPagerListener();
		viewPager.setOnPageChangeListener(listener);
		viewPager.setCurrentItem(1);
	}
	private Calendar nowCalendar = Calendar.getInstance();
	int year;
	int month;
	class MyPagerListener implements OnPageChangeListener{

		int currentPosition;
		@Override
		public void onPageScrollStateChanged(int state) {
			 if (state != ViewPager.SCROLL_STATE_IDLE) return;

             // 当视图在第一个时,将页面号设置为图片的最后一张。
             if (currentPosition == 0) {
                 viewPager.setCurrentItem(mLists.size() - 2, false);

             } else if (currentPosition == mLists.size() - 1) {
                 // 当视图在最后一个是,将页面号设置为图片的第一张。
            	 viewPager.setCurrentItem(1, false);
             }
             Log.e("Calendar", "onPageScrollStateChanged---index"+index+"");
             if(currentPosition-index==1||(index==3&¤tPosition==1)){  //确定上一个页面和当前页面的值,来确定月份应该加还是减
            	 	gridAdapter.add();
            	 	 if(month==1){  //1月的时候,年份-1,月份为12
      		    	   month = 12;
      					year = year -1;
      				}else {  
      					month = month-1;
      				} 
      		       diary_title.setText(year+"年"+month+"月");
            	 	gridAdapter.notifyDataSetChanged();
            	 	index=currentPosition;
             }else if (index-currentPosition==1||(index==1&¤tPosition==3)) {
            	 gridAdapter.sub();
            	 if(month==12){    //1月的时候,年份-1,月份为12
  		    	   month = 1;
  					year = year +1;
  				}else {  
  					month = month+1;
  				} 
  			diary_title.setText(year+"年"+month+"月");
         	 	gridAdapter.notifyDataSetChanged();
         	 	index=currentPosition;
            	 index=currentPosition;
			}                
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int position) {
			currentPosition =position;
			 Log.e("Calendar", "onPageSelected"+currentPosition+"");
		}
	}

	
	private void init() {//初始化viewpager中gridview的参数,这里我添加了5个gridview用于循环切换
		year=nowCalendar.get(Calendar.YEAR);
		month= nowCalendar.get(Calendar.MONTH);
		gridAdapter= new CalendarAdapter(this,nowCalendar);
		mLists = new ArrayList<MyGridView>();
		 for (int i = 0; i < 5 ; i++) {  
	            final MyGridView gv = new MyGridView(this);  
	            gv.setAdapter(gridAdapter);
	            gv.setOnItemClickListener(new OnItemClickListener() { //为每一个Gridview添加点击事件

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						gridAdapter.setSelectPositon(position);
						gridAdapter.notifyDataSetChanged();
					}
	            	
				});
	            mLists.add(gv);  
	        }  
		 
	}

	@Override
	public void onClick(View v) { //按钮的点击事件
		if(v == lastButton){
			
			gridAdapter.sub();
			//handler中通知适配器更新,并修改UI界面
			gridAdapter.notifyDataSetChanged();
		       if(month==1){ //1月的时候,年份-1,月份为12
		    	   month = 12;
					year = year -1;
				}else {  
					month = month-1;
				} 
		       diary_title.setText(year+"年"+month+"月");

		}else if(v == nextButton){
			  
			gridAdapter.add();
			gridAdapter.notifyDataSetChanged();
			 if(month==12){    //1月的时候,年份-1,月份为12
		    	   month = 1;
					year = year +1;
				}else {  
					month = month+1;
				} 
			diary_title.setText(year+"年"+month+"月");
		}
	}
}

CalendarAdapter(gridview的adapter)

public class CalendarAdapter extends BaseAdapter {
	private Context context; 
	private TextView ed_item; //gridview中item的textview
	
	private  int todayPosition=-1;//今天的位置
	private  int selectPosition=-1; //选中的位置
	
	private CalendarCompute calendarCompute;
	private Calendar calendar;
	
	private int todayYear; 
	private int todayMonth;
	
	private int nowYear;  //当前显示的年份
	private int nowMon;  //当前显示的月份
	private int nowMonNum;  //当前显示月的天数
	private int oneDayPosition; //当前显示的 月的第一天的位置
	private int lastMonNum;//上一个月的天数
	
	
	private int[]  dataNumber= new int [42] ;
	
	private int [] dataFlag = new int[42];
	
	public CalendarAdapter(Context context){
		
	}
	
	public CalendarAdapter(Context context,Calendar calendar){
		this.context=context;
		this.calendar=calendar;
		calendarCompute = new CalendarCompute();
		
		nowYear = calendar.get(Calendar.YEAR);
		nowMon = calendar.get(Calendar.MONTH)+1;
		
		todayYear=nowYear;
		todayMonth=nowMon;
		
		onCreatCalender();
	}
	  
	public void sub(){
		if(nowMon==1){ //1月的时候,年份-1,月份为12
			nowMon = 12;
			nowYear = nowYear -1;
		}else {  
			nowMon = nowMon-1;
		} 
		onCreatCalender();
		Log.e("Calendar", "sub---nowMon"+nowMon+"");
	}
	
	public void add(){
		if(nowMon==12){ //1月的时候,年份-1,月份为12
			nowMon = 1;
			nowYear = nowYear +1;
		}else {  
			nowMon = nowMon+1;
		}
		onCreatCalender();
		Log.e("Calendar", "add---nowMon"+nowMon+"");
	}
	
	

	public void onCreatCalender(){
		
		selectPosition=-1;
		oneDayPosition = calendarCompute.oneDayPosition(nowYear,nowMon);
		todayPosition = calendar.get(Calendar.DATE)+oneDayPosition-1;
		nowMonNum = calendarCompute.MonNum(nowYear,nowMon);
		
		if(nowMon==1){
			lastMonNum = calendarCompute.MonNum(nowYear-1, 12);
		}else {
			lastMonNum = calendarCompute.MonNum(nowYear, nowMon-1);
		}
		
		//填充上一个月的日期
		for(int i=oneDayPosition-1;i>=0;i--){
			if(i>=0){
				dataNumber[i]=lastMonNum;
				dataFlag[i]=-1;
				lastMonNum--;
			}
		}
		//填充这一个月的日期
		for(int i=1;i<=nowMonNum;i++){
			dataNumber[oneDayPosition]=i;
			dataFlag[oneDayPosition]=1;
			oneDayPosition++;
		}
		//填充下一个月的日期
		if(oneDayPosition<42){
			
			for(int i=oneDayPosition,j=1 ;i<42;i++,j++){
				dataNumber[i]=j;
				dataFlag[i]=-1;
			}
		}
		if(nowYear==todayYear && nowMon==todayMonth){
			
			dataFlag[todayPosition]=2;
		}
	}
	
	public void setSelectPositon(int position){
		selectPosition = position;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataNumber.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return dataNumber[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = View.inflate(context, R.layout.item, null);
		}
		ed_item = (TextView) convertView.findViewById(R.id.ed_item);
		String texString = dataNumber[position]+"";
		ed_item.setText(texString);
		
		//设置天数字体的颜色
		 if(dataFlag[position]==-1){
			 ed_item.setTextColor(Color.RED);
		 }else {
			 ed_item.setTextColor(Color.BLACK);
		 }
		 
		//设置选中日期的背景的颜色和今天日期的背景的颜色
		if (position==selectPosition||dataFlag[position]==2) {
			ed_item.setBackgroundColor(Color.BLUE);
		}else{
			ed_item.setBackgroundColor(Color.WHITE);
		}
		if(position>=35&&dataFlag[position]==-1&&dataFlag[35]==-1){
			ed_item.setVisibility(View.INVISIBLE);
		}else{
			ed_item.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

}

MyViewPagerAdapter

public class MyViewPagerAdapter extends PagerAdapter {

		private List<MyGridView> mLists;  
	    public MyViewPagerAdapter(Context context, List<MyGridView> array) {  
	        this.mLists=array;  
	    }  
	    @Override  
	    public int getCount() {  
	        return mLists.size();  
	    }  
	  
	    @Override  
	    public boolean isViewFromObject(View arg0, Object arg1) {  
	          
	        return arg0 == arg1;  
	    }  
	    @Override  
	    public Object instantiateItem(View arg0, int position)  
	    {  
	        ((ViewPager) arg0).addView(mLists.get(position));  
	        return mLists.get(position);  
	    }  
	  
	    @Override  
	    public void destroyItem(View arg0, int arg1, Object arg2)  
	    {  
	        ((ViewPager) arg0).removeView((View) arg2);  
	    }
}

MyGridView

public class MyGridView extends GridView {

		public MyGridView(Context context) {
		super(context);
		mContext = context;
		setGirdView();
		}

		private Context mContext;

		private void setGirdView() {
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			
			setLayoutParams(params);
			setNumColumns(7);// 设置每行列数
			setGravity(Gravity.CENTER_VERTICAL);// 位置居中
			setVerticalSpacing(1);// 垂直间隔
			setHorizontalSpacing(1);// 水平间隔
			setClickable(true);
			//setBackgroundColor(getResources().getColor(R.color.calendar_background));
		
			WindowManager windowManager = ((Activity)mContext).getWindowManager();

			DisplayMetrics dm = new DisplayMetrics();
			windowManager.getDefaultDisplay().getMetrics(dm);
			int i  = dm.widthPixels / 7;

			int j = dm.widthPixels - (i * 7);
			int x = j / 2;
			setPadding(x, 0, 0, 0);// 居中
		}
	}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页