基本上,我有3个片段:一个片段有listView和editText,另外两个片段在linearLayout中有添加和清除按钮,水平定向,另一个垂直。
配置更改时,应调用onConfigurationChanged()
方法,该方法动态更改布局并替换片段。
问题在于: 旋转后,listView中的数据被初始化 旋转前添加的所有项目都消失了。
我尝试使用setRetainInstance()
方法,但它仍无效。
这是我的代码
MainActivity.java
public class MainActivity extends Activity implements MyControlInterface {
myListFragment check;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
check = (myListFragment) fragmentManager.findFragmentById(R.id.list_container);
if (check == null) {
Configuration a = getResources().getConfiguration();
if (a.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.add(R.id.control_container,new verticalControlFragment());
fragmentTransaction.commit();
} else if (a.orientation == Configuration.ORIENTATION_PORTRAIT)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.add(R.id.control_container,new horizontalControlFragment());
fragmentTransaction.commit();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.replace(R.id.control_container,new verticalControlFragment());
fragmentTransaction.commit();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_container, new myListFragment());
fragmentTransaction.replace(R.id.control_container,new horizontalControlFragment());
fragmentTransaction.commit();
}
}
public void ProcessCommand(int cmd) {
FragmentManager fragmentManager = getFragmentManager();
check = (myListFragment) fragmentManager.findFragmentById(R.id.list_container);
if (cmd==1) {
check.AddItem();
}
else if (cmd==2) {
check.ClearEditBox();
}
}
}
MyListFragment.java
public class myListFragment extends Fragment {
ListView myListView;
EditText myEditText;
private ArrayList<String> items = null;
private ArrayAdapter<String> adapter = null;
private String[] temp = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
//this.setRetainInstance(true);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//setRetainInstance(true);
temp = getResources().getStringArray(R.array.ToDoList);
items = new ArrayList<String>(Arrays.asList(temp));
myListView = (ListView) getActivity().findViewById(R.id.listView1);
myEditText = (EditText) getActivity().findViewById(R.id.editText1);
myEditText.setText("Input new item.");
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,items);
myListView.setAdapter(adapter);
}
public void AddItem()
{
items.add(myEditText.getText().toString());
myEditText.setText("");
myListView.setAdapter(adapter);
}
public void ClearEditBox()
{
myEditText.setText("");
}
}