Android 安卓:我有一个带滑动标签的工具栏。现在,当用户更改选项卡时,如何更改工具栏上的数据

Android 安卓:我有一个带滑动标签的工具栏。现在,当用户更改选项卡时,如何更改工具栏上的数据,android,toolbar,pagerslidingtabstrip,Android,Toolbar,Pagerslidingtabstrip,我有一个带有滑动选项卡的工具栏。现在,当用户更改选项卡时,如何更改工具栏上的数据。使用和。选定的onPageSelected在SlidingTabLayout中被覆盖 toolbar\u search.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" and

我有一个带有滑动选项卡的工具栏。现在,当用户更改选项卡时,如何更改工具栏上的数据。使用和。选定的onPageSelected在SlidingTabLayout中被覆盖

toolbar\u 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>
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;
}

}

如果您使用的是
ViewPager
,则始终可以尝试此选项:

ViewPager mViewPager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
    ...
    @Override
    public void onPageSelected(int position) {
    switch(position){
    case 0 :
      // Do the changes for the 1st page here
    break;
    case 1 :
      // Do the changes for the 2nd page here
    break;
    }   

    }
    ...

   });
}

您可以在加载的每个片段中充气不同的菜单

@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" />


要收听操作事件,您必须覆盖OnCreateOptions菜单

您所说的数据是什么意思?标题文本?请提供更多详细信息,如屏幕截图等…@Antromet菜单items@Hussein添加代码谢谢你的回复。我试过了,但没用。在onPageSelected中添加了以下代码:if(position==0){LayoutInflater充气器=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);View v2=inflater.inflate(R.LAYOUT.toolbar\u search,null);AutoCompleteTextView搜索栏=(AutoCompleteTextView)v2.findviewbyd(R.id.autocomplete);searchBar.setHint(“Hint1”);}您好,我必须更改工具栏内autocompleteTextView中的提示。我已编辑了我的答案,以便从您的片段中仅获取autocompleteTextView。您好,它可以工作。但它仅在应用程序启动时工作。但之后,当我更改选项卡时,提示不会更改。我有两个选项卡。对于第一个选项卡,我将提示设置为“hint1”对于第二个,我将其设置为“hint2”。因此,当应用程序启动时,它将提示设置为“hint2”,之后没有任何更改。您已将代码放入每个片段中,对吗?让我们假设第一个选项卡为fragment1,第二个选项卡为fragment2?尝试将代码放入onCreateView是的,我这样做了。两个片段在onCreateView中都有单独的代码
<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" />