Календарь |
| « Июнь 2015 » | Пн | Вт | Ср | Чт | Пт | Сб | Вс | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| |
|
Статистика |
|
Онлайн всего: 1 Гостей: 1 Пользователей: 0 | |
|
|
| | |
| Главная » 2015 » Июнь » 26
Few days ago I had to solve such problem. I have FragmentA with ScrollView and lot of items in it. One of the middle items should be FragmentB. It's work fine, but on screen rotation FragmentB appears few seconds latter, than FragmentA created. So I lose ScrollView position on screen rotate.
I've tried a lot of ways to solve problem and was almost ready to replace FragmentB with CustomView, but at last find such solution of this problem:
fragment_a.xml:
android:id="@+id/fragment_a"
...>
android:id="@+id/scroll_view"
...>
.../>
android:id="@+id/fragment_b_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
.../>
FragmentA.java
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.fragment_a, ...);
FragmentB fragmentB = (FragmentB) getChildFragmentManager().findFragmentByTag(FragmentB .FRAGMENT_TAG);
Bundle arguments;
if (fragmentB == null) {
arguments = new Bundle();
arguments.putInt("i", 5);
// (put default params)
} else {
arguments = fragmentB.getArguments();
}
fragmentB = new FragmentB ();
fragmentB.setArguments(arguments);
getChildFragmentManager()
.beginTransaction()
.replace(
R.id.fragment_b_container,
fragmentB,
FragmentB .FRAGMENT_TAG
)
.commit();
...
FragmentB.java
int i, j;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
if (arguments != null) {
i = arguments.getInt("i");
j = arguments.getInt("j", -1);
}
}
...
// do stuf with j
...
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getArguments().putInt("i", i);
getArguments().putInt("j", j);
}
|
|
| |
| | |
|
|