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