1

我已经GridView在我的 3 页上放置了ViewPager。这个想法是建立一个日历的月视图,可以根据用户的需要无限滚动。

我的工作主要基于Jon Willis的无限视图分页的实现。但不是实现我自己的View对象;我正在使用GridViews。

这是我对该onPageScrollStateChanged方法的实现:

@Override
public void onPageScrollStateChanged(int state) {

    if (state == ViewPager.SCROLL_STATE_IDLE) {     

        FragmentPagerAdapter adapter = (FragmentPagerAdapter) mMonthPager
                    .getAdapter();

            // Called to obtain reference for the Fragments inside the ViewPager.
        CalendarGridMonthlyFragment oldMinMonthFragment = (CalendarGridMonthlyFragment) adapter
                    .instantiateItem(mMonthPager, 0);
        CalendarGridMonthlyFragment oldMidMonthFragment = (CalendarGridMonthlyFragment) adapter
                    .instantiateItem(mMonthPager, 1);
        CalendarGridMonthlyFragment oldMaxMonthFragment = (CalendarGridMonthlyFragment) adapter
                    .instantiateItem(mMonthPager, 2);

            // Get the selected month for each Fragment.
        final Calendar oldMinMonth = oldMinMonthFragment.getSelectedMonth();
        final Calendar oldMidMonth = oldMidMonthFragment.getSelectedMonth();
        final Calendar oldMaxMonth = oldMaxMonthFragment.getSelectedMonth();

        if (mFocusedPage == 0) {

            final Calendar newMonth = (Calendar) oldMinMonth.clone();
            newMonth.add(Calendar.MONTH, -1);

            oldMinMonthFragment.setSelectedMonth(newMonth);
            oldMidMonthFragment.setSelectedMonth(oldMinMonth);
            oldMaxMonthFragment.setSelectedMonth(oldMidMonth);

        } else if (mFocusedPage == 2) {

            final Calendar newMonth = (Calendar) oldMaxMonth.clone();
            newMonth.add(Calendar.MONTH, 1);

            oldMinMonthFragment.setSelectedMonth(oldMidMonth);
            oldMidMonthFragment.setSelectedMonth(oldMaxMonth);
            oldMaxMonthFragment.setSelectedMonth(newMonth);
            }  

        mMonthPager.setCurrentItem(1, false);
    }
}

这是我的setSelectedMonth方法:

public void setSelectedMonth(Calendar value) {

    mMonth = value.get(Calendar.MONTH);
    mYear = value.get(Calendar.YEAR);

    CalendarGridMonthlyAdapter newAdapter = new CalendarGridMonthlyAdapter(
            mContext, GRID_CHIP, mGridWidth, mGridHeight,
            mMonth, mYear);

    mGridView.setAdapter(newAdapter);
}

无限滑动有效。但是在滚动完成后有一个明显的闪烁——几乎是半秒。

调试告诉我,setAdapter方法是所有这些大惊小怪的罪魁祸首。那么有什么方法可以加快设置新适配器的过程吗?

..或者也许是另一种方式?

提前致谢!

4

0 回答 0