Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android PropertyDetailPagerAdapter,如何正确处理方向更改?_Android_Android Fragments_Android Viewpager_Fragmentstatepageradapter - Fatal编程技术网

Android PropertyDetailPagerAdapter,如何正确处理方向更改?

Android PropertyDetailPagerAdapter,如何正确处理方向更改?,android,android-fragments,android-viewpager,fragmentstatepageradapter,Android,Android Fragments,Android Viewpager,Fragmentstatepageradapter,我有几个片段和一个浏览页面。用户当然可以在“查看页面”中的页面之间来回移动。我的问题是,当用户更改方向时,当前页面会丢失所有数据。但是当用户来回翻页时,会再次检索数据,页面中会填充正确的数据 我已经测试了和张贴的建议。没有一个奏效 这是我的代码: public class PropertyDetailActivity extends AppCompatActivity { Toolbar toolbar; long[] mPropertiesIDs; private Vi

我有几个片段和一个浏览页面。用户当然可以在“查看页面”中的页面之间来回移动。我的问题是,当用户更改方向时,当前页面会丢失所有数据。但是当用户来回翻页时,会再次检索数据,页面中会填充正确的数据

我已经测试了和张贴的建议。没有一个奏效

这是我的代码:

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;
        }
公共类PropertyDetailActivity扩展了AppCompatActivity{
工具栏;
长[]mProperties;
私人寻呼机;
私人寻呼器;
@凌驾
SaveInstanceState上的公共无效(最终捆绑超出状态){
super.onSaveInstanceState(超出状态);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
long ID=getIntent().getExtras().getLong(“ID”);
MPropertiesId=getIntent().getExtras().getLongArray(“PropertiesId”);
setContentView(R.layout.activity\u property\u detail);
设置工具栏();
mPager=(ViewPager)findViewById(R.id.pager);
mPagerAdapter=newpropertyDetailPageRadapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
int index=PropertyArrayList.getIndexOf(mPropertiesIDs,ID);
mPager.setCurrentItem(索引);
}
专用void setupToolbar(){
toolbar=(toolbar)findviewbyd(R.id.toolbar);
设置支持操作栏(工具栏);
最终ActionBar ActionBar=getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);//将使图标可单击,并在图标左侧添加<。
//setHomeButtonEnabled(true);只会使图标可点击,图标背景的颜色作为点击的反馈。
//actionBar.setHomeAsUpIndicator(R.drawable.abc\u ic\u ab\u back\u mtrl\u am\u alpha);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.menu\u属性\u细节,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
//**********************************************************************************************************************
私有类PropertyDetailPagerAdapter扩展了FragmentStatePagerAdapter{
公共财产DetailPageRadapter(碎片管理器fm){
超级(fm);
}
@凌驾
公共片段getItem(int位置){
MPropertiesId=getIntent().getExtras().getLongArray(“PropertiesId”);
PropertyDetailFragment=新的PropertyDetailFragment();
fragment.setPropertyID(mPropertiesId[position]);
返回片段;
}
@凌驾
public int getCount(){
返回mpropertiesId.length;
}
(我不确定我是否能表达得足够好,因为我的英语说得不太好。所以请随意编辑或留下评论:))


提前感谢您的帮助。

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

与主题相关的代码中的注释

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);
    }
}
private int-mPosition=ListView.INVALID_位置;
私有静态最终选定字符串\u KEY=“SELECTED\u position”;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
[...]
位置=位置;
[...]
//如果存在实例状态,请挖掘它以获取有用信息。
//这里的最终目标是,用户永远不会知道将他们的设备转向一边
//做疯狂的生命周期相关的事情。它应该感觉像一些东西伸展开来,
//或者神奇地利用了这个空间,但应用程序中的数据或位置从来都不存在
//实际上,我丢了。
if(savedInstanceState!=null&&savedInstanceState.containsKey(选定的_键)){
//listview可能还没有被填充。实际上执行
//在已完成的加载中插入。
mPosition=savedInstanceState.getInt(选中的_键);
}
[...]
}
@凌驾
SaveInstanceState上的公共无效(束超出状态){
//平板电脑旋转时,需要保存当前选定的列表项。
//未选择任何项目时,位置将设置为Listview.INVALID_位置,
//所以在储存之前检查一下。
if(位置!=列表视图。位置无效){
outState.putInt(选定的_键、位置);
}
super.onSaveInstanceState(超出状态);
}
@凌驾
public void onLoadFinished(加载器、光标数据){
mForecastAdapter.swapCursor(数据);
if(位置!=列表视图。位置无效){
//如果我们不需要重新启动加载程序,并且有一个需要恢复的位置
//要这样做,现在就这样做。
mListView.smoothScrollToPosition(mPosition);
}
}

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

        android:configChanges="orientation|screenSize"