如何使我的慢速ViewPager更快地工作

时间:2019-04-18 12:56:59

标签: android android-viewpager

我创建了Activity来处理ViewPager。刚开始时它运行良好,但是后来我不得不添加更多代码,以使其在选择页面时刷新我的页面。我通过在每次页面更改时调用onResume()方法来处理它。

我已经优化了片段,清除了每个片段中不必要的代码(inside onResume()),但这没有任何结果。这就是我的活动的样子:

public class MainTabsActivity extends AppCompatActivity implements FullList.OnListFragmentInteractionListener, AcceptedList.OnListFragmentInteractionListener, StartedList.OnFragmentInteractionListener {

    private static final int NUM_TABS = 3;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tabs);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOffscreenPageLimit(2);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }
            @Override
            public void onPageSelected(int i) {
                Fragment fragment = ((SectionsPagerAdapter)mViewPager.getAdapter()).getFragment(i);

                if (fragment != null) {
                    Log.e("PAGE SELECTED", " " + i);
                    tabLayout.getTabAt(i).select();
                    fragment.onResume();
                }
            }
            @Override
            public void onPageScrollStateChanged(int i) {

            }

        });

        Intent i = getIntent();
        int pageNum = i.getIntExtra("pagenumber", 0);
        mViewPager.setCurrentItem(pageNum);
        tabLayout.getTabAt(pageNum).select();
        mViewPager.invalidate();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main_tabs, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onListFragmentInteraction(DummyContent.DummyItem item) {

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        private Map<Integer, String> mFragmentTags;
        private FragmentManager mFragmentManager;

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
            mFragmentTags = new HashMap<>();
            mFragmentManager = fm;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {

            switch(position) {
                case 0:
                    return getString(R.string.tab_text_1);
                case 1:
                    return getString(R.string.tab_text_2);
                case 2:
                    return getString(R.string.tab_text_3);
            }
            return null;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    FullList tab1 = new FullList();
                    return tab1;

                case 1:
                    AcceptedList tab2 = new AcceptedList();
                    return tab2;

                case 2:
                    StartedList tab3 = new StartedList();
                    return tab3;

                default:
                    return null;
            }
        }

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            Object obj = super.instantiateItem(container, position);
            if (obj instanceof Fragment) {
                Fragment f = (Fragment) obj;
                String tag = f.getTag();
                mFragmentTags.put(position, tag);
            }
            return obj;
        }

        public Fragment getFragment(int position) {
            String tag = mFragmentTags.get(position);
            if (tag == null) return null;
            return mFragmentManager.findFragmentByTag(tag);
        }
    }
}

我已经添加了代码来调用onResume()方法,但有趣的是,Fragments仅在我从另一个打开此Activity时才加载这么长时间,但在滚动时却不加载在页面之间(然后一切正常),在进行更改之前一切都很好。
有什么方法可以使这项工作更快?

1 个答案:

答案 0 :(得分:0)

您无需编写onPageSelected代码即可更改选项卡选择。

mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);   // this will synchronize our tablayout with your viewpager

您不应直接调用onResume,这是系统事件,并且对此无效对象的调用  不推荐。

根据您的情况,也无需致电mViewPager.invalidate()