1

我一直在 Android 中尝试使用 ViewPager,并注意到 PagerAdapter 直到onCreate() 在我的主 Activity 中完成后才实例化“页面”视图。这似乎使得无法将任何侦听器连接到主 Activity 的任何布局小部件或回调。是否有一个在 onCreate() 之后调用的方法,我可以挂钩以便从 PagerAdapter 访问实例化视图?OnPostResume() 和 OnPostCreate()似乎没有被调用

主要活动

public class mainApp extends Activity
{
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);

        mAdapter = new MyPagerAdapter ();
        mPager = (ViewPager) findViewById (R.id.myfivepanelpager);
        mPager.setAdapter (mAdapter);
        mPager.setCurrentItem (1);

        /* here, I would like to be able to inflate the views 
         * instantiated in mAdapter, however they have not been 
         * instantiated yet even though setCurrentItem() has been called */
    }

    ...
}

寻呼机适配器

public class MyPagerAdapter extends PagerAdapter
{
    private String TAG = "** MyPagerAdapter **";
    View vscreen1;
    View vscreen2;

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


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


    public Object instantiateItem (View collection, int position)
    {
        LayoutInflater inflater = (LayoutInflater) collection.getContext ().getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;

        switch (position)
        {
            case 0:
                  resId = R.layout.screen1;
                        Log.d (TAG, "screen1");
                  // tie in button callbacks here for screen1
            break;

            case 1:
                        resId = R.layout.screen2;                               
                        Log.d (TAG, "screen2");
                  // tie in button callbacks here for screen2
            break;
        }

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


        return view;
    }

   ...

}   
4

0 回答 0