我是Android编程的新手,我有一个问题。在我的应用程序中,我有ViewPager(带有一些片段),在顶部我有带菜单图标的工具栏。当用户选择ViewPager的不同页面时,是否可以显示不同的图标。我知道OnPageChangeListener的onPageSelected方法在用户选择页面时有效,但我不知道如何更改图标。任何人都可以给我这方面的例子或文章。谢谢!
答案 0 :(得分: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);
答案 1 :(得分:1)
这是可能的。如果您有2个页面,请创建2个菜单资源文件res > menu
。 e.g。
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];
}
}