在ViewPager和TabLayout中调用FragmentPagerAdapter时,getItem()运行两次

时间:2017-06-18 12:32:36

标签: android android-fragments android-viewpager android-tablayout fragmentpageradapter

这是我的<div class="listView" id="foundingScroll"> <label style="margin-top:10px">search:</label> <input ng-model="foundingFilter"/> <div infinite-scroll="scrollSearch()" infinite-scroll-container="#foundingScroll"> <div ng-repeat="founding in foundings| myFilter:foundingFilter"><input type="checkbox" value="founding.id" ng-model="foundingsSearch[founding.name]" /><label>{{founding.name}}</label></div></div></div> 适配器类。

Dashboard_adapt

这是ViewPager和TabLayout的MainActivity。

public class Dashboard_adapt extends FragmentStatePagerAdapter {
    Context context;
    ArrayList<Fragment> fragments = new ArrayList<>();


    public Dashboard_adapt(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
        setFragments();
    }
    private void setFragments(){
        fragments.add(new Home_dashboard());
        fragments.add(new Search_dashboard());
        fragments.add(new Add_dashboard());
        fragments.add(new Favourite_dashboard());
        fragments.add(new User_dashboard());
    }


    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        if (object != null) {
            return ((Fragment) object).getView() == view;
        } else {
            return false;
        }
    }

    @Override
    public Fragment getItem(int position) {
        Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

Adapter类在第一次返回两个Toast ..并且每次下一次在ViewPager中打开下一个Fragment类。

1 个答案:

答案 0 :(得分:0)

这是ViewPager的默认行为.. ViewPager还加载“屏幕外”页面以优化滑动手势...... 您可能会看到位置= 0和1的祝酒词。 查看setOffscreenPageLimit方法...以控制“页面”的数量