0
我有一個通用的片段與2的TextView裏面:移動文本與ViewPager.PageTransformer - 失敗
<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>
此佈局使用IntroFragment這個類是在我的ViewPager(PageAdapter)實例化5次:
@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:
...
在我PageTransformer,我希望做一個翻譯上的X爲標題和描述,但這種代碼的行爲是瘋狂:
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.
}
}
});
是否有可能使用類似這樣的通用片段並在特定的小部件內部使用PageTransformer(就像我的情況Title和Description textview)?
我只是想在標題和說明文字視圖上更快地進行X翻譯(在右側或左側取決於幻燈片的方式)。
感謝您的幫助!
感謝您的指示。但是有可能在這5個片段中通過id找到具有相同ID的視圖嗎?因爲我使用相同的片段5次來填充我的ViewPager。 – anthony
作爲參數獲取的視圖與您在onCreateView中返回的視圖相同, – Blackbelt