如何在方向更改时保留选定的微调器/下拉项目的状态?

我在我的代码中使用了一个微调器下拉列表,其中我有4到5个dynamic填充的值,如果我将“苹果”设置为默认值,并从下拉列表中select“橙子”,并将我的屏幕旋转到纵向的横向,回到默认的“苹果”以及与之相关的视图。如何保存状态,使得当我select“橙子”并旋转到横向时,它填充选定的值/保持在相同的选定状态并保持视图完好/填充在与所选值对应的肖像模式中select的视图。 这里是我使用相同的适配器代码:

public class MarketsSpinnerAdapter extends CustomRowAdapter<AdapterRow> { private List<AdapterRow> mRenderList; public MarketsSpinnerAdapter(final Context context, final List<AdapterRow> renderList) { super(context); mRenderList = new ArrayList<AdapterRow>(); mRenderList.addAll(renderList); } @Override protected void setEntries(final List<AdapterRow> renderList) { mRenderList = renderList; } @Override protected List<AdapterRow> getEntries() { return mRenderList; } @Override public View getDropDownView(final int position, final View convertView, final ViewGroup parent) { return getEntries().get(position).getDropDownView(mContext, convertView); } } 

相应片段中的相应用法:

  private void populateCategoryRows(final Cursor cursor) { mCategories.clear(); mAllCategories.clear(); cursor.moveToPosition(-1); Map<String, String> categoryParentNames = new HashMap<String, String>(); int selectedPosition = 0; String previousHeader = ""; String previousAllHeader = ""; while (cursor.moveToNext()) { final int categoryLevel = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.LEVEL)); final String categoryName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.NAME)); final String categoryDisplayName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.DISPLAY_NAME)); if (categoryLevel == 1) { categoryParentNames.put(categoryName, categoryDisplayName); } } cursor.moveToPosition(-1); while (cursor.moveToNext()) { final int categoryLevel = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.LEVEL)); final boolean categoryIsDefault = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.IS_DEFAULT)) == 1; final boolean categoryIsSelected = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.IS_SELECTED)) == 1; final String categoryParent = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.PARENT)); final String categoryName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.NAME)); final String categoryDisplayName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.DISPLAY_NAME)); if (categoryLevel == 2 ) { String categoryParentDisplayName = categoryParentNames.get(categoryParent); if (!categoryParent.equals(previousHeader)) { if (categoryIsSelected) { mCategories.add(new CategoryHeader(categoryParentDisplayName)); previousHeader = categoryParent; } } if (!categoryParent.equals(previousAllHeader)) { mAllCategories.add(new CategoryHeader(categoryParentDisplayName)); previousAllHeader = categoryParent; } if (categoryIsSelected) { mCategories.add(new SpinnerMarketCategoryRow(categoryName, categoryDisplayName, categoryParent)); } mAllCategories.add(new MarketsCategoryCheckableRow(categoryName, categoryDisplayName, categoryIsSelected, categoryIsDefault)); if(categoryIsDefault){ selectedPosition = mCategories.size()-1; } } } mSpinnerAdapter = new MarketsSpinnerAdapter(Application.getAppContext(), mCategories); headerView.setSpinnerAdapter(mSpinnerAdapter); headerView.setSpinnerSelectedItemPosition(selectedPosition); } if (selectedItem instanceof SpinnerMarketCategoryRow) { selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position); } else { if (mSpinnerAdapter.getCount() - 1 >= position + 1) { selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position + 1); } else { selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position - 1); } } final MarketsFragment parentFragment = (MarketsFragment) getParentFragment(); parentFragment.onCategorySelected(selectedCategory.getCategoryName(), selectedCategory.getCategoryParentName()); } @Override public void showResults(final Uri uri) { LayoutUtils.showResults(getView(), headerView.getSpinnerId()); headerView.setVisibility(View.VISIBLE); } @Override public void showNoResults(final Uri uri) { final MarketsFragment parentFragment = (MarketsFragment) getParentFragment(); parentFragment.hideSpinner(); //LayoutUtils.showNoResult(getView(), headerView.getSpinnerId()); } @Override public void onDismiss(DialogInterface dialog) { headerView.setSelected(false); } @Override public void onNothingSelected(IcsAdapterView<?> parent) { } 

有任何想法吗?

谢谢!

Related of "如何在方向更改时保留选定的微调器/下拉项目的状态?"

你可以这样做…

 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("yourSpinner", yourSpinner.getSelectedItemPosition()); // do this for each or your Spinner // You might consider using Bundle.putStringArray() instead } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // initialize all your visual fields if (savedInstanceState != null) { yourSpinner.setSelection(savedInstanceState.getInt("yourSpinner", 0)); // do this for each of your text views } } 

希望这可以帮助

如果设备的configuration(由Resources.Configuration类定义)发生更改,则显示用户界面的任何内容都需要更新以匹配该configuration,因此您的Activity除非另行指定,否则configuration更改(例如更改屏幕方向,语言,input设备等)将导致您的当前活动被破坏,正常执行onPause(),onStop()和onDestroy()的 Activity lifecycle process

如果活动在前台或用户可见,则在该实例中调用onDestroy()后,将创build一个活动的新实例,其中前一个实例从onSaveInstanceState(Bundle)生成的任何savedInstanceState

这是因为任何应用程序资源(包括布局文件)都可以根据任何configuration值进行更改。 在一些特殊情况下(就像你的,如果我正确的话,如果你只有微调/下拉在当前的用户界面,你不需要经历完整的活动生命周期),你可能要绕过重新启动你的活动或更多types的configuration更改。 这是通过其清单中的android:configChanges属性完成的,或者也可以使用onSaveInstanceState(Bundle) ,当activity被销毁并且从begening重新创build时,它被调用。

你只有两种方法来解决这个问题_

1_

    1. 在Activity标签的Manifest文件中添加android:configChanges =“orientation”
  <?xml version="1.0" encoding="utf-8"?> <manifest ... > <application ... > <activity android:name="SpinnerActivity" android:configChanges="orientation" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
  • 2,覆盖onConfigurationChanged ,当您的活动正在运行时,当设备configuration发生变化时,由系统调用。
  @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); int orientation = newConfig.orientation; switch (orientation) { case Configuration.ORIENTATION_LANDSCAPE: // do what you want when user is in LANDSCAPE break; case Configuration.ORIENTATION_PORTRAIT: // do what you want when user is in PORTRAIT break; } } 

2_

使用put方法将值存储在onSaveInstanceState()中:

 protected void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); //Put your spinner values to restore later... savedInstanceState.putLong("yourSpinnerValKey", yourSpinner.getSelectedItemPosition()); } 

并恢复onCreate()中的值:

 public void onCreate(Bundle savedInstanceState) { if (savedInstanceState!= null) { //get your values to restore... value = savedInstanceState.getLong("param"); } } 

这一定会解决您的问题,并且在屏幕方向改变时不会刷新您的微调器。 我希望这会帮助你和所有! 🙂