我有一个表示周数及其无限的ViewPager。
问题是当我向左或向右滑动正确的数据加载时,一秒钟后重新加载从开始的那一周开始的数据。
PageModelWeek.java
public class PageModelWeek
{
private int index;
private String text;
private String monday;
private String mondayValue;
private String tuesday;
private String tuesdayValue;
private String wednesday;
private String wednesdayValue;
private String thursday;
private String thursdayValue;
private String friday;
private String fridayValue;
private String saturday;
private String saturdayValue;
private String sunday;
private String sundayValue;
private Context context;
public PageModelWeek(int index, Context context)
{
this.index = index;
this.context = context;
setIndex(index);
}
public int getIndex()
{
return index;
}
public void setIndex(int index)
{
this.index = index;
setWeek(index);
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
public Context getContext()
{
return context;
}
public void setContext(Context context)
{
this.context = context;
}
private void setWeek(int index)
{
SimpleDateFormat dayOfWeekFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
SimpleDateFormat dayOfMonthFormat = new SimpleDateFormat("dd", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.WEEK_OF_YEAR, index);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
for (int i = 0; i < 7; i++)
{
if (i == 0)
{
monday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
mondayValue = dayOfMonthFormat.format(calendar.getTime());
}
else if (i == 1)
{
tuesday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
tuesdayValue = dayOfMonthFormat.format(calendar.getTime());
}
else if (i == 2)
{
wednesday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
wednesdayValue = dayOfMonthFormat.format(calendar.getTime());
}
else if (i == 3)
{
thursday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
thursdayValue = dayOfMonthFormat.format(calendar.getTime());
}
else if (i == 4)
{
friday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
fridayValue = dayOfMonthFormat.format(calendar.getTime());
}
else if (i == 5)
{
saturday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
saturdayValue = dayOfMonthFormat.format(calendar.getTime());
}
else if (i == 6)
{
sunday = dayOfWeekFormat.format(calendar.getTime()).substring(0, 1).toUpperCase();
sundayValue = dayOfMonthFormat.format(calendar.getTime());
}
calendar.add(Calendar.DATE, 1);
}
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String d = sdf.format(calendar.getTime());
this.text = d;
}
public String getMonday()
{
return monday;
}
public String getMondayValue()
{
return mondayValue;
}
public String getTuesday()
{
return tuesday;
}
public String getTuesdayValue()
{
return tuesdayValue;
}
public String getWednesday()
{
return wednesday;
}
public String getWednesdayValue()
{
return wednesdayValue;
}
public String getThursday()
{
return thursday;
}
public String getThursdayValue()
{
return thursdayValue;
}
public String getFriday()
{
return friday;
}
public String getFridayValue()
{
return fridayValue;
}
public String getSaturday()
{
return saturday;
}
public String getSaturdayValue()
{
return saturdayValue;
}
public String getSunday()
{
return sunday;
}
public String getSundayValue()
{
return sundayValue;
}
}
听众
viewPagerWeeks.setOnPageChangeListener(new OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
mSelectedWeekPageIndex = position;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{}
@Override
public void onPageScrollStateChanged(int state)
{
if (state == ViewPager.SCROLL_STATE_IDLE)
{
final PageModelWeek leftPage = mWeekPageModel[PAGE_LEFT];
final PageModelWeek middlePage = mWeekPageModel[PAGE_MIDDLE];
final PageModelWeek rightPage = mWeekPageModel[PAGE_RIGHT];
final int oldLeftIndex = leftPage.getIndex();
final int oldMiddleIndex = middlePage.getIndex();
final int oldRightIndex = rightPage.getIndex();
// user swiped to right direction --> left page
if (mSelectedWeekPageIndex == PAGE_LEFT)
{
// moving each page content one page to the right
leftPage.setIndex(oldLeftIndex - 1);
middlePage.setIndex(oldLeftIndex);
rightPage.setIndex(oldMiddleIndex);
setWeekContent(PAGE_RIGHT);
setWeekContent(PAGE_MIDDLE);
setWeekContent(PAGE_LEFT);
// user swiped to left direction --> right page
}
else if (mSelectedWeekPageIndex == PAGE_RIGHT)
{
leftPage.setIndex(oldMiddleIndex);
middlePage.setIndex(oldRightIndex);
rightPage.setIndex(oldRightIndex + 1);
setWeekContent(PAGE_LEFT);
setWeekContent(PAGE_MIDDLE);
setWeekContent(PAGE_RIGHT);
}
viewPagerWeeks.setCurrentItem(PAGE_MIDDLE, false);
}
}
});
setContentWeek
private void setWeekContent(int index)
{
final PageModelWeek model = mWeekPageModel[index];
}
适配器
private class ViewPagerWeeksAdaper extends PagerAdapter
{
@Override
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);
}
@Override
public int getCount()
{
// we only need three pages
return 3;
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
LinearLayout ll = (LinearLayout) mInflater.inflate(R.layout.dayview_header_days, null);
PageModelWeek currentWeek = mWeekPageModel[position];
for (int i = 0; i < ll.getChildCount(); i++)
{
LinearLayout llDay = (LinearLayout) ll.getChildAt(i);
TextView tvDayOfWeek = (TextView) llDay.getChildAt(0);
TextView tvDayOfMonth = (TextView) llDay.getChildAt(1);
if (i == 0)
{
tvDayOfWeek.setText(currentWeek.getMonday());
tvDayOfMonth.setText(currentWeek.getMondayValue());
}
else if (i == 1)
{
tvDayOfWeek.setText(currentWeek.getTuesday());
tvDayOfMonth.setText(currentWeek.getTuesdayValue());
}
else if (i == 2)
{
tvDayOfWeek.setText(currentWeek.getWednesday());
tvDayOfMonth.setText(currentWeek.getWednesdayValue());
}
else if (i == 3)
{
tvDayOfWeek.setText(currentWeek.getThursday());
tvDayOfMonth.setText(currentWeek.getThursdayValue());
}
else if (i == 4)
{
tvDayOfWeek.setText(currentWeek.getFriday());
tvDayOfMonth.setText(currentWeek.getFridayValue());
}
else if (i == 5)
{
tvDayOfWeek.setText(currentWeek.getSaturday());
tvDayOfMonth.setText(currentWeek.getSaturdayValue());
}
else if (i == 6)
{
tvDayOfWeek.setText(currentWeek.getSunday());
tvDayOfMonth.setText(currentWeek.getSundayValue());
}
}
container.addView(ll);
return ll;
}
@Override
public boolean isViewFromObject(View view, Object obj)
{
return view == obj;
}
}
当我启动Activity
时调用它private void initWeekPageModel()
{
for (int i = 0; i < mWeekPageModel.length; i++)
{
// init the pagemodel with indexes of -1, 0 and 1
mWeekPageModel[i] = new PageModelWeek(i - 1, this);
}
}
答案 0 :(得分:0)
我认为这一行是你在onPageScrollStateChanged(int state)
:
viewPagerWeeks.setCurrentItem(PAGE_MIDDLE, false);
当我阅读代码时。它会在viewpager“稳定”后立即执行一些操作,然后在最后滚动到初始页面(这是我假设的中间页面)。