APageTransformer
不必影响整体View
,毕竟是你来定义发生的事情。考虑到Fragments
内部的布局ViewPager
看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</RelativeLayout>
<RelativeLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</RelativeLayout>
</LinearLayout>
如您所见,有两个不同RelativeLayouts
的,它们都有一个 id。有了它,您可以调用内部并findViewById()
以不同的方式转换布局的不同部分!View
PageTransformer
public class ExampleTransformer implements ViewPager.PageTransformer {
public void transformPage(View view, float position) {
View topBar = view.findViewById(R.id.top_bar);
View content = view.findViewById(R.id.content);
if (position < -1) {
content.setAlpha(0);
} else if (position <= 1) {
// Now you can fade the content without affecting the topBar
content.setAlpha(...);
// And by setting the translation on the root view you
// can move everything at the same time
view.setTranslationX(...);
} else {
content.setAlpha(0);
}
}
}
我希望我能帮助你,如果你有任何进一步的问题,请随时提问!