有一个片段添加搜索到操作栏

时间:2015-05-27 01:57:34

标签: android android-fragments

我有一个标签式活动,当我在第二个标签中时,我想在操作栏中添加一个搜索图标。我不希望它在第一个标签中显示。我是否需要将其添加到操作栏中,但仅在第二个标签中以某种方式对其进行充气?

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);
}
}

1 个答案:

答案 0 :(得分:1)

在PagerActivity

private MenuItem menuitemMy;

...
..

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {

            menuitemMy.setEnabled(true);

            if( position == 0 ) { menuitemMy.setEnabled(false); }

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {


        }
    });




...
..

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.optionmenu, menu);

        menuitemMy = menu.getItem(0);

        return true;
    }