当我滑动月视图时,我想在我的日历中显示无限的vIEwpager.我怎样才能做到这一点?我想我需要3个片段才能做到这一点.但是,当我向左或向右滑动时,我不知道如何更新月份(片段).我在下面附上了我的代码.我给出了我不知道编码的问号.
这是我的项目:https://github.com/lolyoshi/CalendarWidget
任何帮助表示赞赏.
InfinitedPagerAdapter.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); } }
CalendarVIEw.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))); } } }}
解决方法:
您滑动时月份不更新的原因是您已声明受保护的静态日历日历;静态的.删除静态后,所有三个页面都会显示不同的月份.
保持您的日历视图与github一样,除了此更改:
static CalendarVIEw newInstance(Calendar a) { CalendarVIEw f = new CalendarVIEw(a); return f;}public CalendarVIEw(Calendar cal) { calendar = cal;}
您的适配器将如下所示:
public class InfinitePagerAdapter extends FragmentPagerAdapter{ CalendarVIEw[] fragList; public InfinitePagerAdapter(FragmentManager fm, CalendarVIEw[] fragList) { super(fm); this.fragList = fragList; } @OverrIDe public Fragment getItem(int position) { return fragList[position]; } @OverrIDe public int getCount() { return fragList.length; }}
最后在您的Calendar小部件中修改如下:
@OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_calendar_Widget); final 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 (arg0 == VIEwPager.SCRolL_STATE_IDLE) { if (mSelectedPageIndex < PAGE_MIDDLE) { fragList[0].onPrevIoUsMonth(); fragList[1].onPrevIoUsMonth(); fragList[2].onPrevIoUsMonth(); } else if (mSelectedPageIndex > PAGE_MIDDLE) { fragList[0].onNextMonth(); fragList[1].onNextMonth(); fragList[2].onNextMonth(); } 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); }
这就是你需要做的.一切都应该按照需要运作.
注意:尽管一切正常,但克隆和传递日历对象可能不是最好的选择.日历对象很昂贵.如果可能的话,你应该绕过时间.从here获取一些动力
总结以上是内存溢出为你收集整理的如何在Android中使用ViewPager进行无限制的寻呼机全部内容,希望文章能够帮你解决如何在Android中使用ViewPager进行无限制的寻呼机所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)