当我滑动月视图时,我想在我的日历中显示无限的浏览器。我怎样才能做到这一点?我想我需要 3 个片段来做到这一点。但是,当我向左或向右滑动时,我不知道如何更新月份(片段)。我在下面附上了我的代码。我在不知道编码的地方打上问号。这是我的项目:https ://github.com/lolyoshi/CalendarWidget
任何帮助表示赞赏。
InfinitePagerAdapter.java
public class InfinitePagerAdapter extends FragmentPagerAdapter{
CalendarView[] fragList;
int fragNumber = 3;
public InfinitePagerAdapter(FragmentManager fm, CalendarView[] fragList) {
super(fm);
this.fragList = fragList;
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
return fragList[position];
}
@Override
public int getCount() {
return fragNumber;
}
public void setCalendar(????) {
//Need to update 3 fragments
??????????????????????
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return false;
}
}
CalendarWidget.java
public class CalendarWidget extends FragmentActivity {
ViewPager monthPage;
private static final int PAGE_MIDDLE = 1;
private int mSelectedPageIndex = 1;
public Calendar curMonth;
InfinitePagerAdapter monthPageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_layout);
CalendarView[] fragList = new CalendarView[3];
curMonth = Calendar.getInstance();
Calendar prevMonth, nextMonth;
prevMonth = (Calendar) curMonth.clone();
nextMonth = (Calendar) curMonth.clone();
prevMonth.set(Calendar.MONTH, prevMonth.get(Calendar.MONTH) - 1);
nextMonth.set(Calendar.MONTH, nextMonth.get(Calendar.MONTH) + 1);
fragList[0] = CalendarView.newInstance(prevMonth);
fragList[1] = CalendarView.newInstance(curMonth);
fragList[2] = CalendarView.newInstance(nextMonth);
monthPage = (ViewPager)findViewById(R.id.pager);
monthPageAdapter = new InfinitePagerAdapter(getSupportFragmentManager(), fragList);
monthPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
if (mSelectedPageIndex < PAGE_MIDDLE) {
curMonth.add(Calendar.MONTH, -1);
} else if (mSelectedPageIndex > PAGE_MIDDLE) {
curMonth.add(Calendar.MONTH, 1);
}
//Need to update 3 fragments
monthPageAdapter.setCalendar(????);
monthPage.setCurrentItem(1, false);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
mSelectedPageIndex = arg0;
}});
monthPage.setAdapter(monthPageAdapter);
monthPage.setCurrentItem(1, false);
}
}
日历视图.java
public class CalendarView extends Fragment{
protected static Calendar calendar;
protected final Calendar itemMonth;
private CalendarAdapter calendarAdapter;
private LinearLayout calendarLayout;
public ArrayList<String> items;
ArrayList<String> event;
ArrayList<String> desc;
public Handler handler;
private int mMonth, mYear;
private static final int PAGE_LEFT = 0;
private static final int PAGE_MIDDLE = 1;
private static final int PAGE_RIGHT = 2;
static CalendarView newInstance(?????) {
//What can I put in this function?
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
calendarLayout = (LinearLayout)inflater.inflate(R.layout.calendar_layout, container, false);
final GridView calendarDayGrid = (GridView)calendarLayout.findViewById(R.id.calendar_days_grid);
calendarDayGrid.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.day_cell, getResources().getStringArray(R.array.days_array)));
final GridView calendarGrid = (GridView)calendarLayout.findViewById(R.id.calendar_grid);
calendarGrid.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE){
return true;
}
return false;
}
});
calendarGrid.setOnItemClickListener(new DayItemClickListener());
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
float gridHeight = height*3/4 - 3*(convertDpToPixels(50, getActivity())); //fix the height of table of days in week = 25dp
calendarAdapter = new CalendarAdapter(getActivity(), calendar, gridHeight);
updateCurrentMonth();
calendarGrid.setAdapter(calendarAdapter);
return calendarLayout;
}
public CalendarView() {
calendar = Calendar.getInstance();
itemMonth = (Calendar) calendar.clone();
}
protected void updateCurrentMonth() {
calendarAdapter.refreshDays();
//handler.post(calendarUpdater);
TextView month = (TextView)calendarLayout.findViewById(R.id.month_title);
month.setTextColor(Color.BLACK);
int monthID = calendar.get(Calendar.MONTH);
int[] monthIds = {R.string.january,R.string.february,R.string.march,R.string.april,R.string.may,R.string.june,
R.string.july,R.string.august,R.string.september,R.string.october,R.string.november,R.string.december};
month.setText(monthIds[monthID]);
String tmp = (String) month.getText();
month.setText(tmp + " " + calendar.get(Calendar.YEAR));
}
protected final void onNextMonth() {
if (calendar.get(Calendar.MONTH) == Calendar.DECEMBER) {
calendar.set((calendar.get(Calendar.YEAR) + 1), Calendar.JANUARY, 1);
} else {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
}
updateCurrentMonth(calendar);
}
protected final void onPreviousMonth() {
if (calendar.get(Calendar.MONTH) == Calendar.JANUARY) {
calendar.set((calendar.get(Calendar.YEAR) - 1), Calendar.DECEMBER, 1);
} else {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)-1);
}
updateCurrentMonth(calendar);
}
public static float convertDpToPixels(float dp,Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
public static float convertPixelsToDp(float px,Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
private final class DayItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView dayView = (TextView)view.findViewById(R.id.date_in_cell);
final CharSequence text = dayView.getText();
if (text != null && !"".equals(text)) {
calendarAdapter.setSelected(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), Integer.valueOf(String.valueOf(text)));
}
}
}
}