Foursnakes Foursnakes - 27 days ago 14
Android Question

Dynamically set ViewPager's height with fragments

I'm trying to set

ViewPager
's height dynamically, so that each
Fragment
of the
ViewPager
has its own height. I based my code on the solution found by czaku here: Dynamically set ViewPager height

activity_artwork_page.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_artwork_page"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.andrea.chatbeacon.ArtworkPageActivity">

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="4000dp" />

<fragment
class="com.example.andrea.chatbeacon.ChatFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/chat_fragment"
android:layout_marginTop="40dp"
tools:layout="@layout/fragment_chat" />
</LinearLayout>

</ScrollView>


ArtworkPageActivity.java

public class ArtworkPageActivity extends AppCompatActivity {

private ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artwork_page);

mPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}

@Override
protected void onResume() {
super.onResume();

ViewTreeObserver viewTreeObserver = mPager.getViewTreeObserver();
viewTreeObserver
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout artwork_slide_layout = (LinearLayout) findViewById(R.id.artwork_slide_layout);
int viewPagerWidth = mPager.getWidth();
int viewPagerHeight = artwork_slide_layout.getHeight();

layoutParams.width = viewPagerWidth;
layoutParams.height = viewPagerHeight;

mPager.setLayoutParams(layoutParams);
mPager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}

@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack
super.onBackPressed();
} else {
// Otherwise, select the previous step
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
Fragment fragment = new ArtworkSlideFragment();
Bundle args = new Bundle();
args.putInt("position", position);
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
int id = getIntent().getIntExtra("id", 0);
return Variables.artworks[id].getTitles().length;
}
}
}


fragment_artwork_slide.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/artwork_slide_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ArtworkSlideFragment">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/artwork_image"
android:contentDescription="@string/artwork_image_description" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/artwork_title"
android:textColor="@android:color/black"
android:textSize="18sp"
android:layout_marginTop="@dimen/activity_vertical_margin" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/artwork_text"
android:textColor="@android:color/black"
android:textSize="15sp"
android:layout_marginTop="10dp" />

</LinearLayout>


ArtworkSlideFragment.java

public class ArtworkSlideFragment extends Fragment {

public ArtworkSlideFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

Bundle args = getArguments();
int id = getActivity().getIntent().getIntExtra("id", 0);
int position = args.getInt("position", 0);
View view = inflater.inflate(R.layout.fragment_artwork_slide, container, false);
ImageView image = (ImageView) view.findViewById(R.id.artwork_image);
TextView title = (TextView) view.findViewById(R.id.artwork_title);
TextView text = (TextView) view.findViewById(R.id.artwork_text);

Glide.with(getActivity().getApplicationContext()).load(Variables.artworks[id].getImages()[position]).into(image);
title.setText(Variables.artworks[id].getTitles()[position]);
text.setText(Variables.artworks[id].getTexts()[position]);

return view;
}
}


I tried to set

viewPagerHeight = artwork_slide_layout.getHeight();


but it seems not to work. How can I set the
viewPagerHeight
?

Answer

I found a working solution from vabhishek at:

https://github.com/vabhishek/WrapContentViewPagerDemo

This works even without the CustomScrollView defined by vabhishek.

Comments