【问题标题】:How Do I Dynamically Update ViewPagerIndicator Headers?如何动态更新 ViewPagerIndicator 标头?
【发布时间】:2013-05-25 22:37:58
【问题描述】:

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

我的代码

创建时

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

【问题讨论】:

    标签: android view android-viewpager adapter viewpagerindicator


    【解决方案1】:

    您遇到的主要问题是您的寻呼机适配器和更新方法。我建议您使用现有的库,例如 Jake Wharton 的 this one 或 this one,而不是重写整个内容。它将使您的生活更轻松,并完全按照您的意愿行事。

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

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

    【讨论】:

    • 感谢您的回复。我已经在使用 Jake Wharton 的 ViewPagerIndicator 并且您链接到的教程是我用来指导实现我的 viewpager 的教程。
    • 我没有意识到,因为您的适配器与教程中的适配器看起来非常不同。例如,您没有实现 TitleProvider 或教程中的任何其他方法。我理解适配器/指示器设置工作的方式是标题是从标题数组中提取的。这意味着您设置 textview 的方法行不通。相反,您需要将所需的标题添加到可用标题数组中,然后根据正在显示的片段进行设置。
    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2021-09-20
    • 1970-01-01
    • 2022-11-29
    • 2018-03-30
    • 2019-12-18
    相关资源
    最近更新 更多