我该如何动态更新ViewPagerIndicator的标题?

3

我是一名Android开发新手,我在动态更新Jake Wharton的Viewpagerindicator标题/头部方面遇到了麻烦。我已经尝试在onPageChangeListener方法和另一个方法中使用notifyDataSetChanged()来更新指示器实例,但都没有成功。我最初通过OnCreate中的全局字符串设置标题/头部。我尝试实现这个SO问题的答案,但没有成功。非常感谢任何帮助解决这个问题的人。

我的代码:

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个回答

1
您主要的问题在于分页适配器和更新方法。我建议您使用现有库,比如这个这个(由Jake Wharton开发),而不是重新编写整个代码。它将使您的生活更加轻松,并且完全可以实现您想要做的事情。
这里是一个设置Jake Wharton ViewPagerIndicator的教程链接,提供了示例代码以及几乎一步一步的设置说明。只需根据您的需要修改教程即可。
在教程中,您会注意到他们使用一个Array来保存每个视图将显示的标题。示例中的标题是预定义的。我注意到您的适配器中有一个标题数组。如果这就是您需要设置的全部内容,请使用它们。如果您不知道要提前添加到您的ViewPager中的片段,您可以在将它们添加到适配器时将它们键入 - 例如,使用一个HashMap作为支持适配器的集合,这样您可以像添加此示例中一样那样为每个Fragment命名,然后将这些标题(从哈希映射中的键值)添加到ViewPagerIndicator标题列表中,因为它们出现了。(只需获取显示的片段并在 HasMap 中查找其键)。

谢谢您的回复。我已经在使用Jake Wharton的ViewPagerIndicator,您提供的教程就是我用来实现我的ViewPager的指南。 - B. Money
我没有意识到,因为你的适配器看起来与教程中的适配器非常不同。例如,你没有实现TitleProvider或教程中的任何其他方法。我理解适配器/指示器设置的工作方式是从标题数组中提取标题。这意味着你在设置textview方面的方法行不通。相反,你需要将所需的标题添加到可用标题的数组中,然后根据显示的片段进行设置。 - Rarw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接