我是 android 开发的菜鸟,我在动态更新 Jake Wharton 的 Viewpagerindicator 标题/标题时遇到了问题。我已经尝试在 onPageChangeListener 方法和另一个方法中的指示器实例上使用 notifyDataSetChanged() 但没有成功。我最初在 OnCreate 中通过全局字符串设置标题/标题。我试图实现来自 this 的答案所以问题没有成功。非常感谢任何解决此问题的帮助。
我的代码
创建时
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;
}
最佳答案
您遇到的主要问题是寻呼机适配器和更新方法。与其重写整个东西,我建议你使用现有的库,如 this one或 this one jack 沃顿。它会让你的生活更轻松,并且完全按照你想做的去做。
这是一个link to the turotial了解如何设置 Jake Wharton ViewPagerIndicator。它提供了示例代码和有关如何设置它的分步说明。只需修改教程以满足您的需求。
您会在教程中注意到他们使用数组
来保存将为每个 View 显示的标题。示例中的标题是预定义的。我注意到您的适配器中有一个标题数组。如果这就是您需要设置的全部,请使用它们。如果您事先不知道哪些 fragment 将添加到您的 ViewPager
,您可以在将它们添加到适配器时键入它们 - 例如,使用 HashMap
作为支持您的适配器的集合,您可以为每个 Fragment
添加标题 like in this example然后只需将这些标题(它们是 HashMap 中的键值)添加到 ViewPagerIndicator 标题列表中即可。 (只需获取显示的 fragment 并在 HasMap 中找到它的键)。
关于android - 如何动态更新 ViewPagerIndicator header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16754530/