【问题标题】:PropertyDetailPagerAdapter, how to handle orientation changes correctly?PropertyDetailPagerAdapter,如何正确处理方向变化?
【发布时间】:2015-11-23 15:29:09
【问题描述】:

我有几个片段和一个视图寻呼机。用户当然可以在视图寻呼机的页面之间来回切换。我的问题是,当用户更改方向时,当前页面会丢失所有数据! .但是当用户来回翻页时,再次检索数据并用正确的数据填充页面。

我在thisthis 帖子中测试了建议。没有任何效果。

这是我的代码:

public class PropertyDetailActivity extends AppCompatActivity {
    Toolbar toolbar;
    long[] mPropertiesIDs;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long ID = getIntent().getExtras().getLong("ID");
        mPropertiesIDs = getIntent().getExtras().getLongArray("propertiesIDs");
        setContentView(R.layout.activity_property_detail);
        setupToolbar();
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new PropertyDetailPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        int index = PropertyArrayList.getIndexOf(mPropertiesIDs, ID);
        mPager.setCurrentItem(index);
    }

    private void setupToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon.
//        actionBar.setHomeButtonEnabled(true); will just make the icon clickable, with the color at the background of the icon as a feedback of the click.
//        actionBar.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_property_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


//**********************************************************************************************************************

    private class PropertyDetailPagerAdapter extends FragmentStatePagerAdapter {
        public PropertyDetailPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            mPropertiesIDs = getIntent().getExtras().getLongArray("propertiesIDs");
            PropertyDetailFragment fragment = new PropertyDetailFragment();
            fragment.setPropertyID(mPropertiesIDs[position]);
            return fragment;
        }

        @Override
        public int getCount() {
            return mPropertiesIDs.length;
        }

(我不确定我是否能表达得足够好,因为我不会说英语那么好。所以请随时编辑或发表评论:))

提前感谢您的帮助。

【问题讨论】:

    标签: android android-fragments android-viewpager fragmentstatepageradapter


    【解决方案1】:

    在清单的活动标签中使用此属性,它不会丢失数据。

            android:configChanges="orientation|screenSize"
    

    【讨论】:

      【解决方案2】:

      我的一个应用程序中有这个(在线课程示例)。我无法解释,但我希望这可以帮助你!

      代码中与主题相关的注释

      private int mPosition = ListView.INVALID_POSITION;
      private static final String SELECTED_KEY = "selected_position";
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
      
          [...]
      
          mPosition = position;
      
          [...]
      
          // If there's instance state, mine it for useful information.
          // The end-goal here is that the user never knows that turning their device sideways
          // does crazy lifecycle related things.  It should feel like some stuff stretched out,
          // or magically appeared to take advantage of room, but data or place in the app was never
          // actually *lost*.
          if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) {
              // The listview probably hasn't even been populated yet.  Actually perform the
              // swapout in onLoadFinished.
              mPosition = savedInstanceState.getInt(SELECTED_KEY);
          }
      
      [...]
      
      }
      
      @Override
      public void onSaveInstanceState(Bundle outState) {
          // When tablets rotate, the currently selected list item needs to be saved.
          // When no item is selected, mPosition will be set to Listview.INVALID_POSITION,
          // so check for that before storing.
          if (mPosition != ListView.INVALID_POSITION) {
              outState.putInt(SELECTED_KEY, mPosition);
          }
          super.onSaveInstanceState(outState);
      }
      
      
      @Override
      public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
          mForecastAdapter.swapCursor(data);
          if (mPosition != ListView.INVALID_POSITION) {
              // If we don't need to restart the loader, and there's a desired position to restore
              // to, do so now.
              mListView.smoothScrollToPosition(mPosition);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-07-11
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-16
        • 2010-11-03
        • 1970-01-01
        • 2011-04-11
        相关资源
        最近更新 更多