如何动态更新ViewPagerIndicator标头?

时间:2013-05-25 22:37:59

标签: android view android-viewpager adapter viewpagerindicator

我是Android开发的菜鸟,我无法动态更新Jake Wharton的Viewpagerindicator标题/标题。我已经尝试在onPageChangeListener方法中的指标实例上使用notifyDataSetChanged(),并且在另一个方法中没有成功。我最初通过OnCreate中的全局字符串设置标题/标题。我试图从this问题来实现答案,但没有成功。任何帮助解决这个问题都非常感谢。

我的代码

的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 个答案:

答案 0 :(得分:1)

您遇到的主要问题是寻呼机适配器和更新方法。我建议您使用Jake Wharton的现有库,如this onethis one,而不是重写整个内容。它将使您的生活更轻松,完全符合您的要求。

这是关于如何设置Jake Wharton ViewPagerIndicator的link to the turotial。它提供了示例代码以及有关如何设置的逐步说明。只需修改教程即可满足您的需求。

您将在教程中注意到他们使用Array来保存将为每个视图显示的标题。示例中的标题是预定义的。我注意到你的适配器中有一个标题数组。如果这就是您需要设置的全部内容,请使用它们。如果您不知道哪些片段会提前添加到ViewPager,您可以在将它们添加到适配器时对其进行键入 - 例如,使用HashMap作为支持的集合你的适配器,你可以在添加like in this example时为每个Fragment标题,然后在它们出现时将这些标题(它们的哈希映射中的键值)添加到ViewPagerIndicator标题列表中。 (只需获取显示的片段并在HasMap中找到它的键)。