PagerAdapter类instantiateItem在onCreate中自动被调用两次

时间:2013-03-11 06:46:51

标签: android android-viewpager android-pageradapter

我是viewpager的新手,我想使用viewpager创建视图,这里所有的东西都是动态的,所以我按照这种方式,我的问题是instantiateItem()被调用两次oncreate。
的OnCreate

{
    pager = (ViewPager) findViewById(R.id.panelPager);
    adapter = new MyPagerAdapter();     
    pager.setAdapter(adapter);
}
public class MyPagerAdapter extends PagerAdapter {

        @Override 
        public Object instantiateItem(View collection,int position) {

            Log.d("Pos",""+position);
            //PagerView = new View(collection.getContext());
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            PagerView = inflater.inflate(R.layout.newmainviewpager, null, false);
            lvMenu=(ListView)PagerView.findViewById(R.id.lvMenuItem);
            imgMainItem=(ImageView)PagerView.findViewById(R.id.imgDisplay);
            addMenuItemAdapter.clear();
            addMenuItemAdapter.notifyDataSetChanged();
            addMenuItemAdapter.notifyDataSetInvalidated();
            String str[][] = datasource.GetSubMenuDetailsFromMenuId(MenuIdlst
                    .get(position).trim());
            Log.d("Str", "" + str.length);
            SubMnuIdlst.clear();
            SubMnuNamelst.clear();
            ArabicSubMnulst.clear();
            TypeIdlst.clear();
            TypeNamelst.clear();
            for (int i = 0; i < str.length; i++) {

                SubMnuIdlst.add(""+str[i][0]);
                SubMnuNamelst.add(""+str[i][2]);
                ArabicSubMnulst.add(""+str[i][3]);
                Log.d("SubMenuId",""+str[i][0]);
                Log.d("SubMenuName",""+str[i][2]);
                Log.d("SubMenuNameArabicMenu",""+str[i][3]);
            }
            String [][]TypeDetails=datasource.GetTypeDetailsFromMenuId(MenuIdlst.get(position));
            for (int i = 0; i < TypeDetails.length; i++) {
                TypeIdlst.add(TypeDetails[i][0]);
                TypeNamelst.add(TypeDetails[i][3]);
                Log.d("TypeId",""+TypeDetails[i][0]);
                Log.d("TypeName",""+TypeDetails[i][3]);

            }

            for(int i=0;i<SubMnuIdlst.size();i++)
            {
                //Pricelst.clear();
                for(int j=0;j<TypeIdlst.size();j++)
                {
                    String Price=datasource.getPriceFromSubMenuIdAndTypeId(TypeIdlst.get(j),SubMnuIdlst.get(i));
                    //Pricelst.add(Price);      
                    Log.d("Adaper",MenuIdlst.get(i)+","+SubMnuIdlst.get(i)+","+SubMnuNamelst.get(i)+","+
                            TypeIdlst.get(j)+","+TypeNamelst.get(j)+","+Price);
                    addMenuItemAdapter.add(MenuIdlst.get(i)+","+SubMnuIdlst.get(i)+","+SubMnuNamelst.get(i)+","+
                                        TypeIdlst.get(j)+","+TypeNamelst.get(j)+","+Price);
                }
            }

            byte[] photo =datasource.getImagePathFromSubMenuId(SubMnuIdlst.get(position));
            ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);
            Bitmap theImage= BitmapFactory.decodeStream(imageStream);
            Bitmap bitmapScaled = Bitmap.createScaledBitmap(theImage, 100,80, true);
            Drawable drawable = new BitmapDrawable(bitmapScaled);
            imgMainItem.setBackgroundDrawable(drawable);

            lvMenu.setAdapter(addMenuItemAdapter);

            ((ViewPager) collection).addView(PagerView, 0);
            return PagerView;

        }

        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) {
                ((ViewPager) view).removeView((View) view);
        }


/**
 * Determines whether a page View is associated with a specific key object
 * as returned by instantiateItem(ViewGroup, int). This method is required
 * for a PagerAdapter to function properly.
 * @param view Page View to check for association with object
 * @param object Object to check for association with view
 * @return
 */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((View) object);
        }


    /**
     * Called when the a change in the shown pages has been completed.  At this
     * point you must ensure that all of the pages have actually been added or
     * removed from the container as appropriate.
     * @param arg0 The containing View which is displaying this adapter's
     * page views.
     */
        @Override
        public void finishUpdate(ViewGroup arg0) {}


        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}

        @Override
        public Parcelable saveState() {
                return null;
        }

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

    }

3 个答案:

答案 0 :(得分:3)

这是两次调用它的行为,因为视图页面进行下一个视图并存储它以便从当前到下一个快速切换,基本上它使其左右元素用于当前视图,以便您可以向任何方向滚动页面,在开始页面,因为没有左页,这就是为什么它只被调用两次。 您也可以更改您想要创建和保留的视图的数量,但总的来说,我认为最小(不确定,但实际上有些时候)是3

答案 1 :(得分:1)

可能是你的viewpager嵌套在视图组中,视图组依赖于其他视图(例如,使用layout_weight设置的LinearLayout或者为了计算其高度和/或宽度而引用其他视图的RelativeLayout)?如果是这样,这可能是由于父视图的多个布局请求,因为它取决于其他视图。如果这是原因,而不是尝试优化您的布局。

答案 2 :(得分:0)

解决此问题的其他方法是覆盖定义要在寻呼机内加载的元素数量的getCount()方法。

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