在viewpagers`片段里面的元素

时间:2015-01-07 12:44:24

标签: java android android-activity android-fragments android-animation

我有问题。我有一个里面有3个片段的viewpager。在第一个片段中,我有一些ImageViews。

首先如何使用计时器显示图像视图?我使用下面的代码,但我有错误,看起来像:变量' mImageView'从内部类中访问,需要声明为class。

mImageView.setVisibility(View.INVISIBLE);
mImageView.postDelayed(new Runnable() {
          public void run() {
                 mImageView.setVisibility(View.VISIBLE);
          }
}, 5000);

我该如何解决这个问题?

第二次当用户开始从第一个片段滚动到下一个片段时,我试图通过X值移动元素(ImageViews)。它工作,但当我去最后三维片段应用程序崩溃。那么为什么会这样呢?!

MainActivity.java

pager.setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View page, float position) {
                // transformation here
                final float normalizedPosition = Math.abs(Math.abs(position) - 1);
                page.setAlpha(normalizedPosition);

                int pageWidth = page.getWidth();

                if (position < -1) { // [-Infinity,-1)
                    // This page is way off-screen to the left.
                    page.setAlpha(0);

                } else if (position <= 1) { // [-1,1]
                    ImageView mImageView = (ImageView)findViewById(R.id.imageView2);
                    mImageView.setTranslationX((float) (-(1 - position) * 1.7 * pageWidth));


                    mImageView.setVisibility(View.INVISIBLE);
                    mImageView.postDelayed(new Runnable() {
                        public void run() {
                            mImageView.setVisibility(View.VISIBLE);
                        }
                    }, 5000);

                    // The 0.5, 1.5 values you see here are what makes the view move in a different speed.
                    // The bigger the number, the faster the view will translate.
                    // The result float is preceded by a minus because the views travel in the opposite direction of the movement.

                }
                else{ // (1,+Infinity]
                    // This page is way off-screen to the right.
                    page.setAlpha(0);
                }
            }
        });

第三:当用户滚动时,是否可以按圈子移动元素。需要任何帮助!

2 个答案:

答案 0 :(得分:0)

关于你的第一个问题,正如我在评论中所说,你需要使mImageView变量最终

final ImageView mImageView = (ImageView)findViewById(R.id.imageView2);

然后,可能会引起空指针异常(正如Blackbelt所说),因为你正在使用activity的findViewById方法,而且你需要的imageView可能在片段视图中:

final ImageView mImageView = (ImageView) page.findViewById(R.id.imageView2);

对于你的3º问题,请通过“按圈子移动”解释你的意思,然后我会用答案更新我的帖子(如果可以的话)。

答案 1 :(得分:0)

<强> MainActivity.java

// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

<强> ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private final int PAGES = 3;
    private String[] titles={"News", "Organizations", "Map"};

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new TabFragment1();
            case 1:
                return new TabFragment2();
            case 2:
                return new TabFragment3();
            default:
                throw new IllegalArgumentException("The item position should be less or equal to:" + PAGES);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    @Override
    public int getCount() {
        return PAGES;
    }
}

正如您在每个页面中看到的,我有单独的片段文档。下面是其中一个:

public class TabFragment1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab_1, container, false);
    }
}

<强> fragment_tab_1.xml

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

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/city"
        android:id="@+id/imageView2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>