繁体   English   中英

在ViewPager中使用多个图像

[英]Using multiple images in ViewPager

我想使用ViewPager,以便用户可以查看多个图像,并通过滑动屏幕来浏览它们。 我通过遵循Android开发者课程来完成此任务。 但是,我无法弄清楚如何显示不同的图像,它总是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;
        }
    }
}

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

@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
    ...  
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM