在ViewPager

时间:2015-08-21 19:56:36

标签: java android android-viewpager

我想使用ViewPager,以便用户可以查看多个图像,并通过滑动屏幕进行导航。我按照Android Developers lesson完成了这项工作。但是,我无法弄清楚如何显示不同的图像,它总是在布局中显示N时间的第一个图像。我怎么能改变这个?

额外的问题:滑动效果很好,但转换有很多口吃,就像手机无法处理它们一样。我正在使用旗舰手机测试这个以及更多,这可能导致这种口吃?

tutorial.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tutorialLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/img1"
    android:id="@+id/tut1"/>

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/img2"
    android:id="@+id/tut2"/>


</LinearLayout>

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical">


    <Button
        android:layout_width="164dp"
        android:layout_height="62dp"
        android:text="Tutorial"
        android:id="@+id/tutorialBtn"
        android:layout_row="2"
        android:layout_column="1"
        android:alpha="1"
        android:background="@android:drawable/btn_default"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1" />


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/black"
        android:scaleType="fitXY"
        android:id="@+id/blackScreen"
        android:visibility="gone"
        android:layout_gravity="center" />

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"                 
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

</FrameLayout>

MainMenu.java

public class MainMenu extends FragmentActivity {
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);

        mPager = (ViewPager) findViewById(R.id.pager);

        tutorial = (Button) findViewById(R.id.tutorialBtn);
        tutorial.setVisibility(View.GONE);
        tutorial.setAlpha(0f);
        tutorial.getLayoutParams().width = btnWidth;
        tutorial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                black.setVisibility(View.VISIBLE);
                mPager.setVisibility(View.VISIBLE);
                mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
                mPager.setAdapter(mPagerAdapter);

            }
        });


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

        @Override
        public Fragment getItem(int position) {
            return new ScreenSlidePageFragment();
        }

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


    static public class ScreenSlidePageFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            ViewGroup rootView = (ViewGroup) inflater.inflate(
                    R.layout.tutorial, container, false);

            return rootView;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

显示您所拥有的代码会有所帮助,但在您在适配器中创建片段时,关键可能是设置正确的图像:

@Override
public Fragment getItem(int position) {
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
    Bundle arguments = new Bundle();
    arguments.putInt("position", position);
    fragment.setArguments(arguments);
    return fragment;
}

然后在你的片段中:

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
    ...
    Bundle arguments = getArguments(); 

    int position = arguments.getInt("position");

    //display the image at the correct position
    ...  
}

我无法在没有任何代码的情况下对口吃进行评论,但您是否有可能在每个片段中做很多工作?就像加载每个可能的图像一样?