之前有用viewpager加gridview实现过日历的翻页和选中效果,但是尤其是滑动效果不太流畅,现在改成线性布局,比较完美的实现了这个效果,并且对viewpager的无限循环也做了复用处理。viewpager在内存中会保存左中右三个页面的view,我们只需要将这三个view放在一个list里,循环的时候 复用刷新数据即可,原理类似listview的view池的概念。
主要代码:
package com.example.datedemo;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv_title;
private ViewPager vp;
private TextView tv_last;
private List list_v = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_title = (TextView) findViewById(R.id.view_title);
vp = (ViewPager) findViewById(R.id.vp);
Calendar calendar = Calendar.getInstance();
tv_title.setText(calendar.get(Calendar.YEAR) + "." + (calendar.get(Calendar.MONTH) + 1) + "."
+ calendar.get(Calendar.DAY_OF_MONTH));
for (int i = 0; i < 3; i++) {
LinearLayout container = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.view_ll, null);
for (int j = 0; j < 7; j++) {
LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.view_ll_h, null);
if (j == 0) {
((TextView) linearLayout.getChildAt(0)).setText("周日");
((TextView) linearLayout.getChildAt(1)).setText("周一");
((TextView) linearLayout.getChildAt(2)).setText("周二");
((TextView) linearLayout.getChildAt(3)).setText("周三");
((TextView) linearLayout.getChildAt(4)).setText("周四");
((TextView) linearLayout.getChildAt(5)).setText("周五");
((TextView) linearLayout.getChildAt(6)).setText("周六");
} else {
for (int k = 0; k < 7; k++) {
final TextView tv = (TextView) linearLayout.getChildAt(k);
LinearLayout.LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
layoutParams.height = getScreenWidth(this)/7;
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!TextUtils.isEmpty(tv.getText())) {
if (tv_last != null) {
tv_last.setBackgroundResource(0);
}
String[] strs= tv_title.getText().toString().split("\\.");
strs[2] = tv.getText().toString();
tv_title.setText(strs[0]+"."+strs[1]+"."+strs[2]);
tv.setBackgroundResource(R.drawable.bg_text);
tv_last = tv;
}
}
});
}
}
container.addView(linearLayout);
}
list_v.add(container);
}
vp.setAdapter(new Myadapter());
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH,arg0 -Integer.MAX_VALUE/2);
tv_title.setText(calendar.get(Calendar.YEAR) + "." + (calendar.get(Calendar.MONTH) + 1) + "."
+ calendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
vp.setCurrentItem(Integer.MAX_VALUE/2);
}
class Myadapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LinearLayout ll = list_v.get(Math.abs(position-Integer.MAX_VALUE/2) % 3);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, position-Integer.MAX_VALUE/2);
calendar.set(Calendar.DAY_OF_MONTH, 1);
int FirstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int countOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int index = 0;
switch (FirstDayOfWeek) {
case Calendar.SUNDAY:
index = 0;
break;
case Calendar.MONDAY:
index = 1;
break;
case Calendar.TUESDAY:
index = 2;
break;
case Calendar.WEDNESDAY:
index = 3;
break;
case Calendar.THURSDAY:
index = 4;
break;
case Calendar.FRIDAY:
index = 5;
break;
case Calendar.SATURDAY:
index = 6;
break;
default:
break;
}
int weekIndex = 1;
int dayIndex = index;
for (int i = 1; i < countOfMonth + 1; i++) {
LinearLayout ll_week = (LinearLayout) ll.getChildAt(weekIndex);
((TextView) ll_week.getChildAt(dayIndex)).setText(i + "");
if ((dayIndex + 1) % 7 == 0) {
weekIndex++;
dayIndex = 0;
} else {
dayIndex++;
}
}
if (ll.getParent() != null) {
((ViewGroup) ll.getParent()).removeView(ll);
}
container.addView(ll);
return ll;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
}
}
/**
* 获取屏幕的宽度px
*/
public static int getScreenWidth(Context context) {
DisplayMetrics metric = new DisplayMetrics();
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getMetrics(metric);
return metric.widthPixels;
}
}
最后是下载链接:
http://download.csdn.net/detail/feiyang877647044/9499546