本文介绍了恢复微调位置片段,屏幕旋转后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如我在标题写的标题,我想恢复选定的位置微调,屏幕旋转之后。试图通过保存包的第做到这一点,并在旋转后获得 savedInstanceState
位置
As I wrote in title the title, I want restore selected position in spinner, after screen rotation. Tried do this by saving position in bundle, and get position from savedInstanceState
after rotation in
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { }
或
onViewCreated(View view, Bundle savedInstanceState)
或
onActivityCreated(Bundle savedInstanceState)
但没有人作品: - /
but no one works :-/
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());
}
}
我也试过 setSelection(位置,FALSE)
是假
推荐答案
如果全部失败,你为什么不使用静态变量?!
If all that failed, why don't you use a static variable?!
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);
这篇关于恢复微调位置片段,屏幕旋转后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!