2013-05-25 84 views
3

我是Android開發的小菜鳥,我無法動態更新傑克沃頓的Viewpagerindicator標題/標題。我已經嘗試在onPageChangeListener方法內的指示器實例上使用notifyDataSetChanged(),並且在另一個方法中沒有成功。我最初通過OnCreate中的全局字符串設置標題/標題。我試圖從this實施答案,但沒有成功。任何解決這個問題的幫助都非常感謝如何動態更新ViewPagerIndicator標題?

我的代碼

的OnCreate

chosenAilment = ""; 
    suggestedRemedy=""; 
    whichAilment=chosenAilment; 
    whichRemedy=suggestedRemedy; 
    whichArea="AREA OF DISCOMFORT"; 

    setContentView(R.layout.viewpagermain); 
    MyPagerAdapter adapter = new MyPagerAdapter(this); 
    pager = (ViewPager)findViewById(R.id.viewpager); 
    indicator = (TitlePageIndicator)findViewById(R.id.indicator); 
    pager.setAdapter(adapter); 
    indicator.setViewPager(pager); 
    indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      Crouton.makeText(RemedyActivity.this, "page changed", Style.INFO).show(); 
      if(position==0){ 
       whichAilment=""; 
       whichRemedy=""; 
       whichArea="AREA OF DISCOMFORT"; 
      }else if(position==1){ 
       whichAilment=chosenAilment; 
       whichRemedy=""; 
       whichArea=""; 
      }else if(position==2){ 
       whichAilment=""; 
       whichRemedy=suggestedRemedy; 
       whichArea=""; 
      } 
      indicator.notifyDataSetChanged(); 
     } 

更新方法

public void updateMsg(String t_info, float t_x, float t_y, int t_c){ 
    //infoView.updateInfo(t_info, t_x, t_y, t_c); 
    popUpButton.setText(TouchView.touchInfo);  
    popUpButton.setX(t_x-(popUpButton.getWidth()/2)); 
    popUpButton.setY(t_y-(popUpButton.getHeight())); 

    Resources res = getResources(); 
     if(TouchView.touchInfo=="Arm"){ 
      ailmentsList = res.getStringArray(R.array.arm_ailments); 
      chosenAilment = "ARM AILMENTS"; 
     }else if(TouchView.touchInfo=="Leg"){ 
      ailmentsList = res.getStringArray(R.array.leg_ailments); 
      chosenAilment = "LEG AILMENTS"; 
     }else if(TouchView.touchInfo=="Back"){ 
      ailmentsList = res.getStringArray(R.array.back_ailments); 
      chosenAilment = "BACK AILMENTS"; 
     }else if(TouchView.touchInfo=="Groin"){ 
      if(isMale){ 
       ailmentsList = res.getStringArray(R.array.groin_ailments_male); 
       chosenAilment = "GROIN AILMENTS"; 
      }else{ 
       ailmentsList = res.getStringArray(R.array.groin_ailments_female); 
       chosenAilment = "GROIN AILMENTS"; 
      } 
     }else if(TouchView.touchInfo=="Chest"){ 
      if(isMale){ 
       ailmentsList = res.getStringArray(R.array.chest_ailments_male); 
       chosenAilment = "CHEST AILMENTS"; 
      }else{ 
       ailmentsList = res.getStringArray(R.array.chest_ailments_female); 
       chosenAilment = "CHEST AILMENTS"; 
      } 
     }else if(TouchView.touchInfo=="Abdomen"){ 
      if(isMale){ 
       ailmentsList = res.getStringArray(R.array.abdomen_ailments_male); 
       chosenAilment = "ABDOMEN AILMENTS"; 
      }else{ 
       ailmentsList = res.getStringArray(R.array.abdomen_ailments_female); 
       chosenAilment = "ABDOMEN AILMENTS"; 
      } 
     }else if(TouchView.touchInfo=="Head"){ 
      ailmentsList = res.getStringArray(R.array.head_ailments); 
      chosenAilment = "HEAD AILMENTS"; 
     }else if(TouchView.touchInfo=="Buttocks"){ 
      ailmentsList = res.getStringArray(R.array.buttocks_ailments); 
      chosenAilment = "BUTTOCKS AILMENTS"; 
     }else{ 
      ailmentsList = res.getStringArray(R.array.head_ailments); 
      chosenAilment = ""; 
     } 

    indicator.notifyDataSetChanged(); 
    } 

尋呼機適配器

private class MyPagerAdapter extends PagerAdapter { 

    private String[] titles = new String[] {whichArea, whichAilment, whichRemedy }; 
    private final Context context; 
    private int[] scrollPosition = new int[titles.length]; 

    public MyPagerAdapter(Context context) 
    { 
     this.context = context; 
     for (int i = 0; i < titles.length; i++) 
     { 
      scrollPosition[i] = 0; 
     } 
    } 

    @Override 
    public String getPageTitle(int position) 
    { 
     return titles[position]; 
    } 

    @Override 
    public int getCount() 
    {    
     return titles.length; 
    } 

回答

1

主要你有問題,與您的尋呼機適配器和updat e方法。我建議您不要使用現有的庫,如傑克沃頓的this onethis one。它會讓你的生活變得更加輕鬆,並且完全符合你所要做的。

以下是關於如何設置傑克沃頓ViewPagerIndicator的link to the turotial。它提供了示例代碼和接近分步說明如何設置它。只需修改教程以滿足您的需求。

您將會在本教程中注意到,他們使用Array來保存將爲每個視圖顯示的標題。示例中的標題是預定義的。我注意到你的Adapter中有一個titles數組。如果這就是你需要設置的所有東西,請使用這些。如果您不知道預先將什麼片段添加到您的ViewPager,那麼您可以在將它們添加到適配器時將它們鍵入,例如,使用HashMap作爲支持適配器的集合,使您可以標題爲每個Fragment,因爲它添加了like in this example,然後只需將這些標題(它們來自散列映射的鍵值)添加到ViewPagerIndicator標題列表中即可。 (只需獲取顯示的片段並在HasMap中找到它的關鍵字)。

+0

感謝您的回覆。我已經在使用Jake Wharton的ViewPagerIndicator,並且您鏈接的教程是我用來實現viewpager的指南。 –

+0

我沒有意識到,因爲你的適配器看起來與教程中的非常不同。例如,您不需要在教程中實現TitleProvider或任何其他方法。我理解適配器/指示器的工作方式是標題從標題數組中取出。這意味着你在設置textview的方法是行不通的。相反,您需要將所需標題添加到可用標題數組中,然後根據所顯示的片段進行設置。 – Rarw