2
我有一個選項卡式活動,並且我想在第二個選項卡中將搜索圖標添加到操作欄。我不希望它在第一個標籤中顯示。我是否需要將它添加到操作欄中,但只是在第二個選項卡中以某種方式誇大它?有片段添加搜索到操作欄
Tab2.java
public class Tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_2,container,false);
return v;
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[];
int NumbOfTabs;
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
@Override
public Fragment getItem(int position) {
if(position == 0)
{
Tab1 tab1 = new Tab1();
return tab1;
}
else
{
Ta2b tab2 = new Tab2();
return tab2;
}
}
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
@Override
public int getCount() {
return NumbOfTabs;
}
}
menu_launch.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".LaunchActivity">
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:src="@drawable/ic_action_search"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
LaunchActivity.java
public class LaunchActivity extends ActionBarActivity {
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Tab 1","Tab 2"};
int Numboftabs =2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.white);
}
});
tabs.setViewPager(pager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_launch, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這對我不起作用。它說「無法解析符號'viewPager'」我會發布更多的代碼。 – yawers
將尋呼機的名稱從「viewPager」更改爲「尋呼機」 – eurosecom
那麼,我要將其添加到我的LaunchActivity.java或ViewPagerAdapter.java? – yawers