【问题标题】:Moving text with ViewPager.PageTransformer - failed使用 ViewPager.PageTransformer 移动文本 - 失败
【发布时间】:2015-05-28 07:30:12
【问题描述】:

我有一个通用片段,里面有 2 个文本视图:

<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 文本视图)?

我只想在标题和描述文本视图上更快地进行 X 翻译(在右侧或左侧取决于幻灯片方式)。

感谢你们的帮助!

【问题讨论】:

    标签: android android-fragments android-pagetransformer


    【解决方案1】:

    我只想更快地进行 X 平移(在右侧或左侧 取决于幻灯片方式)标题和描述文本视图。

    是的,这是可能的。 position 向右移动时大于零,向左移动时小于零。在您的代码中,速度因子 是硬编码的 (1.2)。您必须找到适合您需要的数学函数或一组值。

    【讨论】:

    • 感谢您的指示。但是是否有可能在这 5 个片段中通过 id 找到具有相同 id 的视图?因为我使用相同的片段 5 次来填充我的 ViewPager。
    • 您作为参数获得的视图与您在 onCreateView 中返回的视图相同,
    猜你喜欢
    • 2015-01-30
    • 1970-01-01
    • 2015-08-29
    • 2013-12-17
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2012-08-25
    相关资源
    最近更新 更多