I have a generic fragment with 2 textview inside:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/background_image"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="150dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/title"
style="@style/IntroTitle"
android:text="TITLE"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/description"
style="@style/IntroDescription"
android:text="Description"
android:layout_marginTop="8dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
</LinearLayout>
</RelativeLayout>
This layout is used by IntroFragment and this class is instanciated 5 times in my ViewPager (PageAdapter) :
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
switch (position) {
case 0:
fragment = IntroFragment.newInstance(getString(R.string.intro_title_screen_1),
getString(R.string.intro_desc_screen_1));
break;
case 1:
fragment = IntroFragment.newInstance(getString(R.string.intro_title_screen_2),
getString(R.string.intro_desc_screen_2));
break;
case 2:
...
In my PageTransformer, I want to do a translation on X for the title and for the description, but the behavior with this code is crazy:
mViewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position <= -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
} else if (position <= 1) { // [-1,1]
TextView textview = (TextView) view.findViewById(R.id.description);
textview.setTranslationX((float) (-(1 - position) * 1.2 * pageWidth));
textview = (TextView) view.findViewById(R.id.title);
textview.setTranslationX((float) (-(1 - position) * 1.2 * pageWidth));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
}
}
});
Is it possible to use a generic fragment like that and to use a PageTransformer on a specific widget inside (like in my case Title and Description textview) ?
I just want to do an X translation faster (on the right or on the left depends the slide way) on title and description textview.
Thanks for your help guys !
yes it is possible.
position
is grater than zero when you move on the right, and smaller than zero when you move on the left. In your code the velocity factor is hardcoded (1.2
). You have to find a mathematical function or a sets of values that suits your needs.