android:在循环viewpager中使用时,列表框闪烁

时间:2012-06-20 23:43:18

标签: android adapter android-viewpager

我的日历应用程序存在此问题,这是可以理解的,但我不知道如何解决它!

我有一个用作无限寻呼机的视图寻呼机(页面数设置为3,当我向左或向右滑动时,我移动页面的内容)。我使用3个相同的视图(查看当天的事件)。每个视图/日由事件列表视图表示。 listview有一个适配器,用于自定义显示事件。

我在viewpager上实现了onPageScrollStateChanged。当向左(前一天),当onPageScrollStateChanged结算时,我将数据从中心日移动到右边,左边的数据移动到中心,我生成前一天的视图设置为左边,最后我使用SetCurrentItem返回中心(允许无限滚动)。 我的问题是列表视图的内容闪烁!!我清楚地看到我从左边的页面传递到中心的页面是相同的但是中心页面还没有调用listview适配器,所以当我强制位置为1时,我的印象是列表在第一次加载时重新加载...(因为它由适配器填充)。 我该如何解决这个问题?

有些代码可能更容易理解:

活动类:

DayPagerAdapter dayAdapter;
ViewPager myViewPager;
int focusedPage;

 @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.OnCreate(savedInstanceState);
    setContentView(R.layout.main);

    dayAdapter = new DayAdapter(this);
    myViewPager = (ViewPager) findViewyId(R.id.mypager);
    myViewPager.setAdapter(dayAdapter);

    myViewPager.setOnPageChangeListener(new onPageChangeListener(){
   ....
 @Override
public void onPageScrollStateChanged(int state) { 
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (mFocusedPage == 0) 
{
   View dayBefore = (View) myViewPager.getChildAt(0);
   View dayCurrent = (View) myViewPager.getChildAt(1);
   View dayNext = (View) myViewPager.getChildAt(2);
   View newView = (View) dayAdapter.istantiateItem(myViewPager,0);
  //move data
  ListView lvBef = (ListView) dayBefore.findViewById(R.id.lvDay);
  ListView lCur = (ListView) dayCurrent.findViewById(R.id.lvDay);
  ListView lvNext = (ListView) dayNext.findViewById(R.id.lvDay);
  ListView lvNew = (ListView) newView.findViewById(R.id.lvDay);

  lvNext.setAdapter(lvCur.getAdapter());
  lvCur.setAdapter(lvBef.getAdapter());
  lvBefore.setAdapter(lvNew.getAdapter());

  dayAdapter.notifyDataSetChanged();
}                                    
else if (mFocusedPage == 2)  
{
  ..........
}    
// go back to the center allowing to scroll indefinitely
mDayPager.setCurrentItem(1, false);

ViewPager适配器类

class DayAdapterextends PagerAdapter  {

ViewPager container;
Context theContext;
LayoutInflater inflater;

public DayAdapter(Context activityContext)
{
theContext = activityContext;
inflater = (LayoutInflater)theContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeViewAt(position);
}

@Override
public void finishUpdate(View container) {

}

@Override
public int getCount() {
return 3;
}

 @Override
public Object instantiateItem(View container, int position) {

    View newview = infater.inflate(R.layout.dayLayout, null);
    ListView lvListViewDay = (ListView) newview.findViewById(R.id.lvDay);
    ArrayList events = getSomeEvents();
    DayEventAdapter dayAdap = new DayEventAdapter(newview.getContext(), events);
    lvListViewDay.setAdapter(adapSat);
    container.addView(newview,position);
    return newview;
}

@Override
public boolean isViewFromObject(View view, Object object) {
 return view == object;
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {

}   

@Override
public Parcelable saveState() {
  return null;
}

@Override
public void startUpdate(View container) {

}

ListView适配器类

public class DayEventAdapter extends BaseAdapter 
....
public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null)
  {
   convertView = layoutInflater.inflate(R.layout.event_cell, null);
  }

  //get the event
  CalendarEvent event = (CalendarEvent) getItem(position);
  if(event != null)
  {
    ((TextView) convertView.findViewById(R.id.tvEventCellEventHour)).setText(event.getEvDate());
    ((TextView) convertView.findViewById (R.id.tvEventCellEventDesc)).setText(event.getEvDesc());
  }
  return convertView;
}

请给我一个帮助,或者如果我的问题不明确,请不要犹豫! 欢呼声

0 个答案:

没有答案