2012-08-09 68 views
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); 
} 

}

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

+0

我认为你必须根据你的要求设置适配器,假设如果用户先选择课程,然后适配器将只配置两个页面数据,并再次设置myPager.setAdapter(适配器); ,如果用户选择第二课,则适配器将使用3-9页数据进行配置,并再次设置myPager.setAdapter(适配器);. – rajpara 2012-08-09 14:42:14

+0

听起来很合理。好吧,我会试试这个并报告回来。 – Tastybrownies 2012-08-09 15:16:24

+0

好吧,我试图做到这一点,并对使用什么方法感到困惑。我没有太多的页面查看器的经验。到目前为止,如果我有:myPager.setCurrentItem(2);为什么没有setEndingItem? :) – Tastybrownies 2012-08-09 15:39:17

回答

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 

我认为像这样的结构更好地组织。

+0

我终于可以得到一切工作与您的建议。再次感谢你! – Tastybrownies 2012-08-09 17:57:43