2017-02-13 52 views
0

我有這個「框架」應用程序與工具欄和viewPager。Android的viewPager不改變標題欄中的標題

BaseActivity:

public abstract class BaseActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(getLayoutResource()); 
    } 


    protected abstract int getLayoutResource(); 
} 

ToolbarActivity:

protected static Toolbar toolbar; 


@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    View contentView = findViewById(R.id.flToolbarContentContainer); 
    if (contentView instanceof ViewGroup) { 
     ((ViewGroup) contentView) 
       .addView(LayoutInflater.from(this) 
         .inflate(getToolbarLayoutResource() 
           , (ViewGroup) contentView, false)); 
    } 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
} 

@Override 
protected int getLayoutResource() { 
    return R.layout.activity_toolbar; 
} 

protected abstract int getToolbarLayoutResource(); 

public void showToolbar() { 
    toolbar.setVisibility(View.VISIBLE); 
} 

public void hideToolbar() { 
    toolbar.setVisibility(View.GONE); 
} 

    public static void setTitle(String title){ 
     toolbar.setTitle(title); 
    } 
} 

MainActivity:

public class MainActivity extends ToolbarActivity { 


    @Override 
    protected int getToolbarLayoutResource() { 
     return R.layout.activity_main; 
    } 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     TabFragment homeFragment = new TabFragment(); 
     getSupportFragmentManager() 
       .beginTransaction() 
       .replace(R.id.llMainActivityContainer, homeFragment) 
       .commit(); 
    } 

和。 TabFragment:

public class TabFragment extends Fragment { 

    public static TabLayout tabLayout; 
    public static ViewPager viewPager; 
    public static int int_items = 3 ; 
    private String[] titles = new String[3]; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     titles[0] = "Összes"; 
     titles[1] = "Saját"; 
     titles[2] = "Közelben"; 

     /** 
     *Inflate tab_layout and setup Views. 
     */ 
      View x = inflater.inflate(R.layout.view_pager,null); 
      tabLayout = (TabLayout) x.findViewById(R.id.tabs); 
      viewPager = (ViewPager) x.findViewById(R.id.viewpager); 

     /** 
     *Set an Apater for the View Pager 
     */ 
     viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 

     /** 
     * Now , this is a workaround , 
     * The setupWithViewPager dose't works without the runnable . 
     * Maybe a Support Library Bug . 
     */ 

     tabLayout.post(new Runnable() { 
      @Override 
      public void run() { 
        tabLayout.setupWithViewPager(viewPager); 
        } 
     }); 


     viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

      } 

      @Override 
      public void onPageSelected(int position) { 
       ToolbarActivity.setTitle(""+titles[position]); 
      } 

      @Override 
      public void onPageScrollStateChanged(int state) { 

      } 
     }); 
     return x; 

    } 

    class MyAdapter extends FragmentPagerAdapter { 

     public MyAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     /** 
     * Return fragment with respect to Position . 
     */ 

     @Override 
     public Fragment getItem(int position) 
     { 
      switch (position){ 
       case 0 : return new PrimaryFragment(); 
       case 1 : return new SecondFragment(); 
       case 2 : return new ThirdFragment(); 
      } 
     return null; 
     } 

     @Override 
     public int getCount() { 

      return int_items; 

     } 

     /** 
     * This method returns the title of the tab according to the position. 
     */ 

     @Override 
     public CharSequence getPageTitle(int position) { 

      return titles[position]; 
     } 
    } 

我的問題是,如果我打開從viewPager一個新的活動,並完成它, 當我回到viewPager活動使用onPageSelected監聽器運行,但不會改變我的工具欄標題。

你能幫我解決這個問題嗎?

謝謝!

回答

0

嘗試

getActivity.setTitle(""+titles[position]); 
+0

謝謝!你保存我的日子 – David

+0

不客氣:) –