3

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

我的代码

创建时

    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;
    }
4

1 回答 1

1

您遇到的主要问题是寻呼机适配器和更新方法。与其重写整个东西,我建议你使用现有的库,比如这个或Jake Wharton 的这个。它将使您的生活更轻松,并完全按照您的意愿行事。

以下是有关如何设置 Jake Wharton ViewPagerIndicator 的教程链接它提供了示例代码和有关如何设置它的逐步说明。只需修改教程以满足您的需求。

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

于 2013-05-31T14:11:10.253 回答