其他分享
首页 > 其他分享> > android – ViewPagerIndicator不平滑,有1000页

android – ViewPagerIndicator不平滑,有1000页

作者:互联网

现在我有一个ViewPager和ViewPagerIndicator来在Android上分页我的屏幕.

我从FragmentStatePagerAdapter重写getCount()并返回1000以获得1000页.我需要做一些代码来获取日历上的页面标题(dd / MM / yyyy).每当我滚动时,我都会看到所有1000个页面标题都被重建(我在Adapter#getPageTitle(int)上打印一个日志).

这使得我的寻呼机滚动非常慢,不再平滑.

我认为当我滚动1页时,ViewPagerIndicator不应该重建所有页面标题.

更新:添加适配器的源代码

public class ResultAdapter extends FragmentStatePagerAdapter {
    public ResultAdapter(FragmentManager fm) {
        super(fm);
    }

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


    @Override
    public Fragment getItem(int position) {
        Log.d("xskt", "Adapter.GetItem.position=" + position);
        Calendar calendar = Utilities.selectedProvince.getLastDay(Utilities.selectedCalendar, Utilities.pagerSize - position - 1);
        ResultView resultView = new ResultView(Utilities.selectedProvince, calendar);
        resultView.setTitle(calendar.get(Calendar.DAY_OF_MONTH) + "/" + (calendar.get(Calendar.MONTH) + 1));
        return resultView;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Calendar calendar;

                //DO SOME CALCULATE WITH CALENDAR

        // String title = calendar.get(Calendar.DAY_OF_MONTH) + "/" + (calendar.get(Calendar.MONTH) + 1);
        // return title;
        Log.d("xskt","get page title");
        return ((ResultView) getItem(position)).getTitle();
    }

}

解决方法:

我需要同样的东西,这里是我所做的代码,基本上我根据新片段的位置计算要显示的标题.

默认情况下,会显示与当天相对应的片段,如果位置发生变化,我只需获取当前位置和新位置之间的差异,然后修改日期以进行相应显示.

public class RateFragmentPagerAdapter extends FragmentStatePagerAdapter{

private final int todayPosition;
private RateFragment currentFragment;
private final Calendar todayDate;

/** Constructor of the class */
public RateFragmentPagerAdapter(FragmentManager fm, int today_position, Calendar today_date) {
    super(fm);
    todayPosition = today_position;
    todayDate = (Calendar) today_date.clone();
}

/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {     
    currentFragment = new RateFragment();
    Bundle data = new Bundle();

    data.putInt("current_page", arg0);
    currentFragment.setArguments(data);
    return currentFragment;
}

/** Returns the number of pages */
@Override
public int getCount() {     
    return RateDayApplication.numberOfDays;
}

@Override
public CharSequence getPageTitle(int position) {
    Calendar newDate = (Calendar) todayDate.clone();

    int diffDays = position - todayPosition;
    newDate.add(Calendar.DATE, diffDays);

    return RateDayApplication.dateTitleFormat.format(newDate.getTime());
}   

}

它很快,因为我从不调用片段对象,我只是比较当前位置(最终)和函数中的新位置.

标签:android,android-viewpager,viewpagerindicator
来源: https://codeday.me/bug/20190709/1412320.html