I'd like to add 7 previously created views dynamically to my viewpager.
What I try to achieve at the end is a part of my screen (50dp height, match_parent width) looking like this.
| VIEW 1 | VIEW 2 |
When I swipe to the right, the displayed items will be VIEW 2 and VIEW 3, next swipe will be VIEW 3 and VIEW 4 and so on.
My problem is that my 7 views are already created in the onCreateView of my main fragment. I'd like to add them dynamically to my fragment defined in the FragmentStatePagerAdapter.
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private View view1Layout;
private View view2Layout;
public MyPagerAdapter(FragmentManager fm, View view1Layout, View view2Layout) {
super(fm);
this.view1Layout = view1Layout;
this.view2Layout = view2Layout;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new MyFragment();
LinearLayout rootLayout = ((LinearLayout) fragment.getView().getRootView());
switch (position % 7) {
case 0:
rootLayout.addView(view1Layout);
rootLayout.addView(view2Layout);
break;
case 1:
// ....
break;
}
return fragment;
}
@Override
public int getCount() {
return 1000;
}
}
public static class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_dashboard_line, container, false);
}
}
As you can imagine, it will crash when I try to get the rootLayout, as the fragment is not instantiated at this time. Is there a good way to achieve what I want or a workaround if not ?
Thanks !
Aucun commentaire:
Enregistrer un commentaire