我是Android開發的小菜鳥,我無法動態更新傑克沃頓的Viewpagerindicator標題/標題。我已經嘗試在onPageChangeListener方法內的指示器實例上使用notifyDataSetChanged(),並且在另一個方法中沒有成功。我最初通過OnCreate中的全局字符串設置標題/標題。我試圖從this實施答案,但沒有成功。任何解決這個問題的幫助都非常感謝如何動態更新ViewPagerIndicator標題?
我的代碼
的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;
}
感謝您的回覆。我已經在使用Jake Wharton的ViewPagerIndicator,並且您鏈接的教程是我用來實現viewpager的指南。 –
我沒有意識到,因爲你的適配器看起來與教程中的非常不同。例如,您不需要在教程中實現TitleProvider或任何其他方法。我理解適配器/指示器的工作方式是標題從標題數組中取出。這意味着你在設置textview的方法是行不通的。相反,您需要將所需標題添加到可用標題數組中,然後根據所顯示的片段進行設置。 – Rarw