雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

ナビゲーションバーをつけるときのメモ

  • ここでは、AppCompatActivityを継承したクラスであることを想定
  • googleMapがデフォルトで表示されているアプリを作った際のものです。

MainActivity.java

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    private ListView vListView;
    ActionBarDrawerToggle mDrawerToggle;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        // 以下サイドバー関連
        vListView = (ListView) findViewById(R.id.listview);
        String[] strings = {"all", "red ■", "blue ■", "green ■","yellow ■"};
        int[] colors = { Color.BLACK,Color.RED, Color.BLUE, Color.GREEN,Color.YELLOW};
        SideBarAdapter customAdapter = new SideBarAdapter(this, android.R.layout.simple_list_item_1, strings, colors );
        vListView.setAdapter(customAdapter);
        vListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               
            }

        });

        //ナビゲーションドロワーの設定
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerToggle = new ActionBarDrawerToggle(this,mDrawer, R.string.open, R.string.close){
            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
//                setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
//                setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawer.setDrawerListener(mDrawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


    }
}

main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="9"
        map:uiZoomControls="true" >

    </fragment>

</LinearLayout>
    <!-- The navigation drawer -->
    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#333" >

        <ListView
            android:id="@+id/listview"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            />

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

Androidアプリ開発逆引きレシピ (PROGRAMMER’S RECiPE)

Androidアプリ開発逆引きレシピ (PROGRAMMER’S RECiPE)