我的日历应用程序有这个问题,这是可以理解的,但我不知道如何解决它!
我有一个用作无限寻呼机的视图寻呼机(页数设置为 3,当我向左或向右滑动时,我会移动页面的内容)。我使用 3 个相同的视图(当天事件的视图)。每个视图/天由事件列表视图表示。listview 有一个用于自定义显示事件的适配器。
我在 viewpager 上实现了 onPageScrollStateChanged。向左(前一天)时,当 onPageScrollStateChanged 设置好时,我将数据从中心一天移到右侧,将左侧的数据移到中心并生成前一天的视图设置到左侧,最后我使用 SetCurrentItem 返回中心(允许无限滚动)。我的问题是列表视图的内容闪烁!我清楚地看到我正在从左侧的页面传递到中心的页面,它们是相同的,但中心页面还没有调用 listview 适配器,所以当我将位置强制为一个时,我的印象是列表在第一次加载时重新加载......(因为它由适配器填充)。我该如何解决这个问题?
有些代码可能更容易理解:
活动类:
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;
}
请给我一个帮助,或者如果我的问题不清楚,请不要犹豫!干杯