gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:35:42 25 4
gpt4 key购买 nike

我在我的代码中使用了一个微调器下拉菜单,其中我有 4 到 5 个动态填充的值,假设我将“苹果”设置为默认值,并且我从下拉菜单中选择“橘子”并将我的屏幕从纵向,它会返回到默认的“苹果”以及与之关联的 View 。如何保存状态,以便当我选择“橘子”并旋转到横向时,它会填充选定的值/保持相同的选定状态并且保持 View 完整/填充在与所选值相对应的纵向模式下选择的 View 。这是我使用的适配器代码:

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);
}

}

各自 fragment 中的对应用法:

 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) {
}

有什么想法吗?

谢谢!

最佳答案

你可以这样做......

@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
}
}

希望对你有帮助

关于android - 如何在方向更改时保留所选微调器/下拉项的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20209015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com