How can I set the height of the ViewPager programmatically more than once?
I can set the height of the ViewPager using vpTabs.getLayoutParams().height = DESIRED_SIZE;
, but only works once at runtime. now the question is how to set the height more than once at runtime?
activity_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlActivityProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.my.package.ProfileActivity">
<!--MainContainer-->
<com.my.package.widget.ScrollViewX
android:id="@+id/svxUserProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Main Container-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Header-->
<RelativeLayout
android:id="@+id/rlProfileBanner"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/listile_green">
<!-- SOME OTHER VIEW-->
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlTabs"
android:layout_below="@id/rlProfileBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Slider-->
<android.support.v4.view.ViewPager
android:id="@+id/vpTabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/stlControl" />
</RelativeLayout>
</RelativeLayout>
</com.my.package.widget.ScrollViewX>
<!--Transparent toolbar-->
<include
android:id="@+id/iAppBar"
layout="@layout/app_bar" />
</RelativeLayout>
The ViewPager is initialized in the onCreate () of the MainActivity class
ViewPager vpTabs = (ViewPager) findViewById(R.id.vpTabs);
I am changing the height of the viewPager from a fragment using an interface like this
//Calling from fragment
int totalHeight = titles.length * EditProfileAdapter.ROW_HEIGHT;
vpListener.onViewPagerHeightChange(totalHeight);
in MainActivity
@Override
public void onViewPagerHeightChange(int height) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.height = height; //left, top, right, bottom
vpTabs.setLayoutParams(params);
}
NOTE. A custom widget ScrollViewX
is the same ScrollView, but with a custom ScrollListener.
source to share
define a new one layoutParams
and set your viewPager to it, then change the height of the layoutParams, this will do the trick, don't do it directly with its original parameters, or the changes won't affect the layout more than once, you also have to make sure the condition you using, right, try using this:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//setting margins around imageimageview
params.height=yourHeight; //left, top, right, bottom
//adding attributes to the imageview
viewPager.setLayoutParams(params);
instead of directly installing it
source to share