1

我目前正在制作一本类似游戏的小书以用于教育目的,并且我正在使用页面查看器将我的 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);
}

}

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

4

1 回答 1

1

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

我认为更好的解决方案是仅根据用户选择的课程将某些页面添加到您的 ViewPager 中。如果您有一个带有用于选择课程的按钮的主活动,那么当单击它们时,它们可以启动不同的活动,其中 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

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

于 2012-08-09T16:57:08.173 回答