我在我的代碼中使用了一個微調器下拉菜單,其中我有4到5個動態填充值,如果我將「蘋果」設置爲默認值,並從下拉列表中選擇「桔子」,並將我的屏幕從畫像,它會回到默認的「蘋果」以及與它相關的視圖。如何保存狀態,使得當我選擇「桔子」並旋轉到橫向時,它會填充選定的值/保持在相同的選定狀態並保持該視圖保持不變/填充在與所選值對應的肖像模式中選擇的視圖。下面是我用相同的適配器代碼:如何在方向更改時保留選定的微調器/下拉項目的狀態?
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) {
}
任何想法?
謝謝!
我的微調是在onCreateOptionsMenu中定義的。我該如何解決這個問題? – TeodorKolev
此解決方案可能會使微調控制器多次調用OnItemSelected()。 –
@SamRamezanli解釋說爲什麼可以多次調用該方法將會有所幫助。 – Elyasin