I got a ViewPager2 widget inside a scrollview which i want to set the height similar to the content of the current shown fragment. The fragments hold either a gridview or a listview which height is set to wrap_content.
The problem is that the gridview gets filled with content after the fragment view was created so the viewpager gets a child height of "0". Also the height of every fragment can be different to the others.
If I set the gridview height to e.g. 500dp everything works fine.
Is there some sort of listener to detect when the gridview is filled with content / the height of the gridview changed to set the viewpager heigth similar to the height of the fragment?
I already tried the solutions described here
How to wrap height of Android ViewPager2 to height of current item?
and
ViewPager2 with differing item heights and WRAP_CONTENT
Layouts:
ViewPager:
<RelativeLayout>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewPagerID"
android:background="@color/light_blue">
</androidx.viewpager2.widget.ViewPager2>
</RelativeLayout>
ScrollView:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/top_navigation_view_bar"
android:id="@+id/center"
android:layout_above="@id/bottom_navigation_view_bar">
<include layout="@layout/layout_viewpager"> </include>
</ScrollView>
Test Fragment:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:id="@+id/grid_fragment_0"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="top"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
</GridView>
</RelativeLayout>
Set up the ViewPager
Function in the activity
private void setUpFragmentsTest(){
String[] fragment_names = {"Fragment0", "Fragment1", "Fragment2"};
mViewPager2 = (ViewPager2) findViewById(R.id.viewPagerID);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
SectionsPagerAdapterNew sectionsPagerAdapterNew = new SectionsPagerAdapterNew(this);
sectionsPagerAdapterNew.addFragment(new Fragment0());
sectionsPagerAdapterNew.addFragment(new Fragment1());
sectionsPagerAdapterNew.addFragment(new Fragment2());
mViewPager2.setAdapter(sectionsPagerAdapterNew);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout, mViewPager2, (tab, position) -> {
tab.setText(fragment_names[position]);
});
tabLayoutMediator.attach();
}
Section pager adapter class:
public class SectionsPagerAdapterNew extends FragmentStateAdapter {
private static final String TAG = "SectionsPagerAdapter";
private final List<Fragment> mFragmentList = new ArrayList<>();
public SectionsPagerAdapterNew(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
public SectionsPagerAdapterNew(@NonNull Fragment fragment) {
super(fragment);
}
public SectionsPagerAdapterNew(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragmentList.get(position);
}
@Override
public int getItemCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment){
mFragmentList.add(fragment);
}
}