I'm trying to implement a ViewPager into my fragment. Here is my Code:
Parent fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_parent, container, false);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
adapter = new CustomPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
return view;
}
Childfragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_child, container, false);
return v;
}
Adapter:
public class CustomPagerAdapter extends FragmentPagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new ChildFragment();
}
@Override
public int getCount() {
return 3;
}
}
So after a while of searching I found out, that I need to use
getChildFragmentManager()
instead of
getFragmentManager()
But nothing happens! With the child fragment is all right, this can be shown as a single fragment, but not in my view pager. Width and height is everywhere "match_parent". The viewPager is in a LinearLayout which is in a scrollView. Could be the scrollview the reason? I don't have any ideas anymore. Can someone help? Thanks a lot! :)
EDIT: I used this tut: How to implement a ViewPager with different Fragments / Layouts
add:
to your scrollview