2012-08-09 66 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