当我在标题中写下标题时,我希望在屏幕旋转后在旋转器中恢复所选位置。尝试通过在捆绑中保存位置来实现此目的,并在轮换后从arr
获取位置
savedInstanceState
或
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { }
或
onViewCreated(View view, Bundle savedInstanceState)
但没有人工作: - /
onActivityCreated(Bundle savedInstanceState)
我还尝试了public class AddRecipeFragment extends Fragment implements ObservableScrollViewCallbacks {
private static final String SELECTED_CATEGORY = "SELECTED_CATEGORY";
private Spinner categoriesSpinner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add_recipe, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
categoriesSpinner = (Spinner) view.findViewById(R.id.categories_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.categories, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categoriesSpinner.setAdapter(adapter);
if (savedInstanceState != null) {
categoriesSpinner.setSelection(savedInstanceState.getInt(SELECTED_CATEGORY, 0), true);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_CATEGORY, categoriesSpinner.getSelectedItemPosition());
}
}
false 。
答案 0 :(得分:0)
如果一切都失败了,为什么不使用静态变量?!
private static int selectedPosition = 0;
选择微调器项目时
categoriesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedPosition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
和onViewCreated()
方法内部
categoriesSpinner.setSelection(selectedPosition);