如何通过页面查看器显示页面间隔

时间:2012-08-09 14:33:30

标签: android android-viewpager

我目前正在为教育目的制作一本类似游戏的小书,我正在使用页面查看器在每页上放置我的Strings.xml中的信息。这是一本相当小的书,所以我这样做。我显示了所有页面,您可以轻松浏览而不会出现任何问题。当我只想显示一定的页面间隔时,我的问题就出现了。游戏中有三节课。在第一课中有2页(1-2),第二课(3-9)和第三课(10-15)。共有15页的信息。

有没有办法让页面查看器只显示一定的页面间隔?

我让用户选择一个按钮,这会将选择了哪一课的意图发送给另一个名为BookActivity的类。从那里我意图找出点击了哪一课,并希望根据它显示某些页面。这是bookActivity类。

    public class BookActivity extends Activity
{
    private String getLessonName;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = getIntent();
        // getting object's properties from LoginActivity class.

        getLessonName = intent.getStringExtra("nameOfLesson");
        MyPageAdapter adapter = new MyPageAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);

       // Button addBasicQuestionsForFirstLesson = (Button)findViewById(R.id.addBasicQuestionsButton);
        myPager.setAdapter(adapter);

        if(getLessonName.equals("one"))
        {
        myPager.setCurrentItem(0);
        if(myPager.getCurrentItem() == 1)
        {
            myPager.setCurrentItem(1);
        }
        }
        else if(getLessonName.equals("two"))
        {
            myPager.setCurrentItem(2);
            if(myPager.getCurrentItem() == 8)
            {
                myPager.setCurrentItem(8);
            }
        }
        else if(getLessonName.equals("three"))
        {
            myPager.setCurrentItem(9);
            if(myPager.getCurrentItem()==14)
            {
                myPager.setCurrentItem(14);
            }
        }


    }

}

非常感谢你为我看这个。如果有人知道如何做这样的事,我真的很感激。我不想为单独的页面进行多项活动,如果有更好的方法可以做到这一点....

 class MyPageAdapter extends PagerAdapter

{

public int getCount()
{
    return 15;
}

public Object instantiateItem(View collection, int position)
{

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch (position)
    {
    case 0:

        resId = R.layout.activity_page_one;

        break;
    case 1:
        // second page with button to add questions!
        resId = R.layout.activity_page_two;

        View view = inflater.inflate(resId, null);
        LinearLayout layout = (LinearLayout) view
                .findViewById(R.id.linearbasic);
        Button addButton = (Button) layout
                .findViewById(R.id.addBasicQuestionsButton);
        ((ViewPager) collection).addView(view, 0);

        addButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.d("clicked", "clclcl");
                LessonActivity.
                importQuestions("lessononebasicquestions");
                // LessonActivity.clicked = true;
                // Toast.makeText(getApplicationContext(), "yay",
                // Toast.LENGTH_LONG).show();

            }
        });

        return view;

        // break;
    case 2:

        resId = R.layout.activity_page_three;

        break;
    case 3:
        resId = R.layout.activity_page_four;
        break;
    case 4:
        resId = R.layout.activity_page_five;
        break;
    case 5:
        resId = R.layout.activity_page_six;
        break;
    case 6:
        resId = R.layout.activity_page_seven;
        break;
    case 7:
        resId = R.layout.activity_page_eight;
        break;
    case 8:
        resId = R.layout.activity_page_nine;

        View view2 = inflater.inflate(resId, null);
        LinearLayout layout2 = (LinearLayout) view2
                .findViewById(R.id.linearlife);
        Button lifeCycleButton = (Button) layout2
                .findViewById(R.id.addLifeCycleButton);
        ((ViewPager) collection).addView(view2, 0);

        lifeCycleButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.d("clicked", "clclcl");
                LessonActivity
                .importQuestions("lessontwolifecyclequestions");
                // LessonActivity.clicked = true;
                // Toast.makeText(getApplicationContext(), "yay",
                // Toast.LENGTH_LONG).show();

            }
        });

        return view2;

    case 9:
        resId = R.layout.activity_page_ten;
        break;
    case 10:
        resId = R.layout.activity_page_eleven;
        break;
    case 11:
        resId = R.layout.activity_page_twelve;
        break;
    case 12:
        resId = R.layout.activity_page_thirteen;
        break;
    case 13:
        resId = R.layout.activity_page_fourteen;
        break;
    case 14:
        resId = R.layout.activity_page_fifteen;

        View view3 = inflater.inflate(resId, null);
        LinearLayout layout3 = (LinearLayout) view3
                .findViewById(R.id.lineartools);
        Button toolsButton = (Button) layout3
                .findViewById(R.id.addToolsQuestionsButton);
        ((ViewPager) collection).addView(view3, 0);

        toolsButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.d("clicked", "clclcl");
                LessonActivity.
                importQuestions("lessonthreetoolsquestions");
                // LessonActivity.clicked = true;
                // Toast.makeText(getApplicationContext(), "yay",
                // Toast.LENGTH_LONG).show();

            }
        });

        return view3;

    }
    View view = inflater.inflate(resId, null);

    ((ViewPager) collection).addView(view, 0);

    return view;

}

@Override
public void destroyItem(View arg0, int arg1, Object arg2)
{
    ((ViewPager) arg0).removeView((View) arg2);

}

@Override
public Parcelable saveState()
{
    return null;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1)
{
    // TODO Auto-generated method stub
    return arg0 == ((View) arg1);
}

}

再次感谢你。这是我添加的代码!

1 个答案:

答案 0 :(得分:1)

您可以使用侦听器进行页面更改,然后在用户超出范围时强制用户访问某个页面。使用onPageSelected(),您可以检测到越界页面,然后强制用户访问上一页。 这是听众:http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html

我认为更好的解决方案是仅根据用户选择的课程向ViewPager添加某些页面。如果您有一个带有按钮的主Activity来选择课程,那么当单击它们时,它们可以启动不同的活动,使ViewPagers填充您想要的页面。所以它看起来像:

MainActivity
- Button1
    -Activity with ViewPager for pages 1-2
- Button2
    -Activity with ViewPager for pages 3-9
- Button3
    -Activity with ViewPager for pages 10-15

我认为像这样的结构更有条理。