gpt4 book ai didi

android - viewpager不同页面的不同菜单图标

转载 作者:行者123 更新时间:2023-11-29 20:50:33 27 4
gpt4 key购买 nike

我是 Android 编程的新手,我有一个问题。在我的应用程序中,我有 ViewPager(带有一些 fragment ),在顶部我有带菜单图标的工具栏。当用户选择ViewPager的不同页面时,是否可以显示不同的图标。我知道 OnPageChangeListener 的 onPageSelected 方法在用户选择页面时有效,但我不明白如何更改图标。谁能给我一些例子或文章。谢谢!

最佳答案

这是可能的。如果你有 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>

菜单_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];
}
}

关于android - viewpager不同页面的不同菜单图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29255635/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com