Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using FragmentStatePagerAdapter, Viewpager inside Scrollview

I am using:

  • An activity layout containing fragment added programmatically.
  • Parent fragment has FragmentStatePagerAdapter with view pager, This all content is inside ScrollView.
  • View pager is filled with some child fragments which has Recyclerview.

Issues are:

  • View pager fragment has a list, so I have to programmatically set parent view pager height. But I can't do that.

For that, I have used

viewpager.getLayoutParams().height = 20000;
viewpager.requestLayout();`

and

ViewGroup.LayoutParams params = viewPagerBottom.getLayoutParams();
params.height =  20000;
viewPagerBottom.setLayoutParams(params);

Also, I have tried view pager and view pager fragments height property as wrap_content

  • Can't see view pager content if i dont set scrollview android:layout_height="match_parent" with android:fillViewport="true".

It is parent fragment layout.xml.

<ScrollView
    style="@style/scrollDefaultStyle"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TabLayout .... />

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>
</ScrollView>
like image 250
Khemraj Sharma Avatar asked Sep 24 '17 13:09

Khemraj Sharma


1 Answers

You can customize the ViewPager to resize the ViewPager to it's current page size on page swipe from this answer.

You can use the following code:

 public class WrapContentViewPager extends ViewPager {

        private int mCurrentPagePosition = 0;

        public WrapContentViewPager(Context context) {
            super(context);
        }

        public WrapContentViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
             boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
             if (wrapHeight) {
                 View child = getChildAt(mCurrentPagePosition);
                 if (child != null) {
                 child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                 int h = child.getMeasuredHeight();

                 heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
                }
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

        public void reMeasureCurrentPage(int position) {
            mCurrentPagePosition = position;
            requestLayout();
        }
    }

Declare it in xml:

    <your.package.name.WrapContentViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </your.package.name.WrapContentViewPager>

After that call reMeasureCurrentpage function on page swipe.

    final WrapContentViewPager wrapContentViewPager = (WrapContentViewPager) findViewById(R.id.view_pager);

    wrapContentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    wrapContentViewPager.reMeasureCurrentPage(wrapContentViewPager.getCurrentItem());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

Note: There could be hundreds of rows in ListView. It's not good idea to use wrap_content for ListView. Either you need to give fixed height for the ListView or else you need to use match_parent.

like image 81
Jon Goodwin Avatar answered Nov 16 '22 02:11

Jon Goodwin