1

我是Android編程中的新手,我有一個問題。在我的應用程序中,我有ViewPager(帶有一些片段),在頂部我有帶菜單圖標的工具欄。當用戶選擇不同的ViewPager頁面時是否可以顯示不同的圖標?我知道OnPageChangeListener onPageSelected方法,它在用戶選擇頁面時起作用,但我不明白如何更改圖標。任何人都可以給我關於這個的例子或文章。謝謝!viewpager不同頁面中的不同菜單圖標

+1

試試google ... :) – 2015-03-25 12:29:02

回答

1

在您使用的視圖尋呼機使用的所有片段:

@Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     inflater.inflate(R.menu.grid_to_list, menu); 
     MenuItem item = menu.findItem(R.id.grid_to_list); 
     item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem item) { 
       switch (item.getItemId()) { 
       case R.id.grid_to_list: 

        //Your stuff 
        break; 

       } 
       return false; 
      } 

     }); 
     super.onCreateOptionsMenu(menu, inflater); 
    } 

,並在onCreateView方法寫:

setHasOptionsMenu(true); 
+0

Surender Kumar!我試圖使用這段代碼,我有一個問題,因爲我在這行錯誤MenuItem item = menu.findItem(R.id.grid_to_list); grid_to_list在這裏意味着什麼?這裏我需要使用什麼標識? – 2015-03-25 13:50:13

+0

它的菜單項ID就像我們在我們的活動中使用佈局小部件ID相同的菜單項ID將在這裏使用。 – 2015-03-25 15:24:25

1

這是可能的。如果您有2個頁面,請創建2個菜單資源文件res > menu。例如

menu_one.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="mcrime.com.mcrime.MainActivity"> 



     <item 
      android:id="@+id/send_icon" 
      android:orderInCategory="100" 
      android:icon="@drawable/ic_send_white_24dp" 
      android:title="Send Report" 
      app:showAsAction="always" /> 

    </menu> 

menu_2.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="mcrime.com.mcrime.MainActivity"> 

    <item 
     android:id="@+id/make_a_call" 
     android:orderInCategory="100" 
     android:icon="@drawable/ic_call_white_24dp" 
     android:title="Make Call" 
     app:showAsAction="always" /> 

</menu> 

然後在您的MainActivity:

private ToolBar toolbar; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.your_activity_name); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     setUpVariables(); 

     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

private void setUpVariables(){ 

     ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
     SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tab_layout); 

     viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); 
     slidingTabLayout.setCustomTabView(R.layout.tab_view, R.id.tab_text_view); 
     slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
      @Override 
      public int getIndicatorColor(int position) { 
       return getResources().getColor(R.color.colorAccent) ; 
      } 
     }); 

     slidingTabLayout.setDistributeEvenly(true); 
     slidingTabLayout.setViewPager(viewPager); 

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

      } 

      @Override 
      public void onPageSelected(int position) { 
       if(position == 0){ 
        MCrime.this.setTitle("Your custom title"); 
        toolbar.getMenu().clear(); 
        toolbar.inflateMenu(R.menu.menu_one); 
       } else if(position == 1){ 
        MCrime.this.setTitle("Your custom title"); 
        toolbar.getMenu().clear(); 
        toolbar.inflateMenu(R.menu.menu_2); 
       } 
      } 

      @Override 
      public void onPageScrollStateChanged(int state) { 

      } 
     }); 

    } 

您需要創建您的尋呼機適配器

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

     String [] titles = {"Title One", "Title Two", }; 

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

     @Override 
     public Fragment getItem(int position) { 
      Fragment fragment; 
      if(position == 0){ 
       fragment = new FragmentOne(); 
       return fragment; 
      } 

      if(position == 1){ 
       fragment = new FragmentTwo(); 
       return fragment; 
      } 


      return null; 
     } 

     @Override 
     public int getCount() { 
      return 2; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return titles[position]; 
     } 
    }