停止在android中的Orientation Change上重新加载ListView

时间:2013-09-25 09:06:17

标签: android listview android-orientation

我有一个ListView,从XML填充。一切正常,但是当我改变设备的方向(Portrait< - > Landscape)时,它会重新加载ListView。我想阻止它。我使用了onSaveInstanceStateonConfigurationChanged。但它仍在重新加载。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        alrowDataView = savedInstanceState.getParcelableArrayList("name");
    //  Toast.makeText(MainActivity.this,alrowDataView.size() , Toast.LENGTH_LONG).show();

        // lvrowDataView=(ListView) findViewById(R.id.list_xml);
    }
    else{
        Toast.makeText(MainActivity.this,"NOdata" , Toast.LENGTH_LONG).show();

    setContentView(R.layout.activity_main);
    lvrowDataView = (ListView) findViewById(R.id.list_xml);
    ParsingXml parsing = new ParsingXml();

之后的一般ListView操作...

以下是OnSavedInstanceStateonConfigurationChanged的代码。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(MainActivity.this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(MainActivity.this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putParcelableArrayList("name", alrowDataView);
    //Toast.makeText(MainActivity.this, alrowDataView.size(),Toast.LENGTH_LONG).show();
}

这些是声明

    private ListView lvrowDataView;
private ArrayList<RowData> alrowDataView;
RowDataAdapter adapterRowData;

我不想使用android:configChanges="orientation"

此外,在onSaveInstanceState中,ArrayListis为空!!!“

1 个答案:

答案 0 :(得分:0)

试试这种方式

android:configChanges="orientation"

不使用最新的android api 3.0 +

你需要像这样使用

<activity
            android:name="YourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
 />