【Android】MenuとToolBar、TabLayout + ViewPagerを実装する。

Menu

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater menuInflater = getMenuInflater();
      menuInflater.inflate(R.menu.main, menu);
      return super.onCreateOptionsMenu(menu);
  }
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/menu_search"
        android:icon="@drawable/action_search"
        android:title="search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>

    <item android:id="@+id/search_history"
        android:title="SearchHistory" />
</menu>

Menuの項目が選択された際の処理を実装する

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search_history:
            Intent intent = new Intent(this, SearchHistoryActivity.class);
            startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

階層的なメニューを表示する。

<item android:id="@+id/file"
    android:title="file" >
    <!-- "file" submenu -->
    <menu>
        <item android:id="@+id/create_new"
            android:title="create_menu" />
        <item android:id="@+id/open"
            android:title="open" />
    </menu>
</item>

ToolBar

Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar); // ToolBarを表示する
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    android:id="@+id/toolBar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>

Backボタン(homeボタン)を有効にする

Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // 戻るボタンが表示される。
getSupportActionBar().setHomeButtonEnabled(true); // 戻るボタンが押せるようになる。

Backボタンの処理を実装する

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra(FROM_SEARCH_HISTORY, "Java");
            startActivity(intent);
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}

TabLayout + ViewPager

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewFragmentPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(searchFragment, "Search");
viewPagerAdapter.addFragments(new TrendFragment(), "Trend");
viewPagerAdapter.addFragments(new SubFragment(), "Sub");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
public class ViewFragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {

    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();

    public ViewFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragments(Fragment fragments, String tabTitles){
        this.fragments.add(fragments);
        this.tabTitles.add(tabTitles);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.atuski.qiitaqlient.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:id="@+id/appBar"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <include android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/main_tool_bar_layout" />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill">
        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_below="@+id/appBar"
        android:layout_height="match_parent"
        android:id="@+id/viewPager">

    </android.support.v4.view.ViewPager>
</RelativeLayout>