android – PagerAdapter类被多次调用

前端之家收集整理的这篇文章主要介绍了android – PagerAdapter类被多次调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个包含ViewPager视图的应用程序,
我创建了一个PagerAdapter,它有视图,PagerAdapter的instantiateItem()方法在create()中被调用两次我不知道为什么,任何人都可以帮我这个吗?

这是我的代码,

View PagerView;
        MyPagerAdapter adapter;
        ViewPager pager;

            adapter = new MyPagerAdapter();     
    pager.setAdapter(adapter);
    pager.setCurrentItem(0);

public class MyPagerAdapter extends PagerAdapter {

        @Override
        public Object instantiateItem(final View collection,final int position) {
            Log.d("Inside","Pager");
            PagerView = new View(collection.getContext());
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            PagerView = inflater.inflate(R.layout.tablemenu,null,false);
            tbMenuDetails = (TableLayout) PagerView
                    .findViewById(R.id.Menutable1);
            scrollview = (ScrollView) PagerView.findViewById(R.id.scrollView1);
            tbMenuDetails.removeAllViews();
            removeTableRows();
            createTableLayout(position);
            String str[][] = datasource.GetSubMenuDetailsFromMenuId(MenuIdlst
                    .get(position).trim());
            Log.d("Str","" + str.length);
            for (int i = 0; i < str.length; i++) {
                addRows(str[i][1],str[i][2],str[i][0],str[i][3],position);
                Log.d("Message","Pos   " + position + "    SubMenuName" + str[i][2]
                        + " SubMenuId" + " " + str[i][0] + " TypeID" + "    "
                        + str[i][3]);
            }

            // View view = inflater.inflate(resId,null);
            ((ViewPager) collection).addView(PagerView,0);

            return PagerView;
        }

        @Override
        public void destroyItem(final View arg0,final int arg1,final Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);

        }

        @Override
        public boolean isViewFromObject(final View arg0,final Object arg1) {
            return arg0 == ((View) arg1);

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void restoreState(Parcelable arg0,ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return MenuIdlst.size();
        }

    }

请帮忙

解决方法

如果您决定使用Fragments,请不要实现PagerAdapter.相反,扩展FragmentPagerAdapter或FragmentStatePagerAdapter:
private class MyPagerAdapter extends FragmentStatePagerAdapter {
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
        // Implement the static method newInstance in MyFragment.java yourself.
        // It should return you a brand new instance of MyFragment,basically using
        // the code you had in your original instantiateItem method.
        return MyFragment.newInstance(position,... etc ...); 
    }
}

然后在你的活动中:

myPagerAdapter = new MyPagerAdapter(getFragmentManager());
// or myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
myViewPager.setAdapter(myPagerAdapter);

要获得有关如何使用Fragments和ViewPagers的更多信息:
https://developer.android.com/reference/android/app/Fragment.html
https://developer.android.com/reference/android/support/v4/view/ViewPager.html
https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

猜你在找的Android相关文章