Android如何在不调用onCreate的情况下更改扩展到ViewPager的布局方向?

时间:2012-09-21 09:59:13

标签: android android-layout android-viewpager viewpagerindicator fragmentpageradapter

我正在使用一个viewpager来加载一些布局并在滑动时做一些事情。我希望能够在活动更改方向时,寻呼机将布局更改为特定方向,而无需再次加载数据。这可能吗 ??

我知道为了避免一遍又一遍地调用onCreate方法,你必须在你的清单中指定下面的行然后使用onConfigurationChanged:

机器人:configChanges = “取向”

但是这样做布局将不会根据方向加载正确的布局(所以如果横向加载横向布局),而是保持加载的布局并调整其大小。

class ViewPagerAdapter extends PagerAdapter {


    public int getCount() {
        return 4;
    }

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

        int resId = 0;
        switch (position) {
        case Constants.LAYOUT_FEEDBACK:
            resId = R.layout.activity_feedback; 
            view = inflater.inflate(resId, null);
            ((ViewPager)collection).addView(view, 0);

            btnSend = (Button)view.findViewById(R.id.btnSend);
            etFeedback = (EditText)view.findViewById(R.id.etFeedback);
            etEmail = (EditText)view.findViewById(R.id.etMail);

            sendFeedback(btnSend);

            break;
        case Constants.LAYOUT_HOME:
            resId = R.layout.activity_home;

            view = inflater.inflate(resId, null);

            ((ViewPager)collection).addView(view, 0);

            btnCamera = (ImageButton)view.findViewById(R.id.btnCamera);
            btnPoo = (ImageButton)view.findViewById(R.id.btnPoo);
            imgPopular = (Button)view.findViewById(R.id.btnPopular);
            imgRecent = (Button)view.findViewById(R.id.btnRecent);
            imgNearby = (Button)view.findViewById(R.id.btnNearby);

            btnEdit = (ImageButton)view.findViewById(R.id.btnEdit);
            btnFeedback = (ImageButton)view.findViewById(R.id.btnFeedback);

            incSearch = getLayoutInflater().inflate(R.layout.include_search, null);
            incNoInternet = view.findViewById(R.id.include_no_internet);
            incNoInternet.setVisibility(View.INVISIBLE);
            btnCancel = (Button)incSearch.findViewById(R.id.btnCancel);
            imgX = (ImageView)incSearch.findViewById(R.id.imgX);
            etKeywords = (EditText)incSearch.findViewById(R.id.etText);

            btnRetry = (Button)incNoInternet.findViewById(R.id.btnTryAgain);

            pooAdapter = new PooAdapter(poos);
            lvPoo = (ListView)view.findViewById(R.id.lvPoo);
            lvPoo.addHeaderView(incSearch);
            lvPoo.setCacheColorHint(Color.TRANSPARENT);
            lvPoo.setAdapter(pooAdapter);

0 个答案:

没有答案