我有一个带滑动标签的工具栏。现在,当用户更改选项卡时,如何更改工具栏上的数据。使用SlidingTabLayout和SlidingTabStrip。在SlidingTabLayout中覆盖了onPageSelected。
toolbar_search.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" >
<AutoCompleteTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawableLeft="@drawable/ic_action_search"
android:hint=" Bus Service Number"
android:textColor="#000" />
</android.support.v7.widget.Toolbar>
SearchByBusInfo.java(主要活动)
public class SearchByBusInfo extends AppCompatActivity {
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[] = { "tab1", "tab2" };
int Numboftabs = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_by_bus_info);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles
// fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles,
Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true,
// This makes the tabs Space Evenly in
// Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ViewPageAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are
// Going to be passed when ViewPagerAdapter is
// created
int NumbOfTabs; // Store the number of tabs, this will also be passed when
// the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in
// the class
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[],
int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
// This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if (position == 0) // if the position is 0 we are returning the First
// tab
{
Tab1 tab1 = new Tab1();
return tab1;
} else // As we are having 2 tabs if the position is now 0 it must be 1
// so we are returning second tab
{
Tab2 tab2 = new Tab2();
return tab2;
}
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
答案 0 :(得分:1)
您可以在加载的每个片段中为其他菜单充气。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main,menu);
}
其中menu_main.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">
<item
android:id="@+id/myAction"
android:orderInCategory="100"
android:icon="@drawable/a_drawable"
app:showAsAction="always" />
要收听Action事件,您必须覆盖OnCreateOptionsMenu
答案 1 :(得分:0)
如果您使用txtConvertir = new JTextField();
ValidarPrecio validarprecio = new ValidarPrecio();
txtConvertir.addKeyListener(validarprecio);
:
ViewPager