読者です 読者をやめる 読者になる 読者になる

【Android】ドロワー

 

f:id:stanleyblenz:20161113210234p:plain

 

-------------------------------------------------------------------------------------------------------------

build.gradle
dependencies {
  compile 'com.android.support:appcompat-v7:24.2.1'
  compile 'com.android.support:support-v4:24.2.1'

}

-------------------------------------------------------------------------------------------------------------

activity_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">
  <!-- メイン用 -->
  <FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
  <!-- ドロワー用 -->
  <!--layout_width"240dp" または "320dp" -->
  <!--layout_gravity="start"(またはleft)左側に表示する場合 -->
  <FrameLayout
    android:id="@+id/drawer"
    android:layout_width="240dp"
    android:layout_gravity="start"
    android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>

-------------------------------------------------------------------------------------------------------------

fragment_drawer.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:text="@string/app_name"
    android:textSize="22dp"
    android:textStyle="bold"
    android:textColor="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center"
    android:background="@color/drawer_background"/>
  <ListView
    android:id="@+id/drawerlistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:background="#ffffff"/>
</LinearLayout>

-------------------------------------------------------------------------------------------------------------

fragment_drawer_adapter.xml

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:id="@+id/drawertitle"
    android:textSize ="16dp"
    android:textStyle="bold"
    android:textColor="#000000"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:paddingLeft="10dp"
    android:background="#ffffff"/>
  <TextView
    android:id="@+id/drawerurl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/drawertitle"
    android:visibility="gone"/>
</RelativeLayout>

-------------------------------------------------------------------------------------------------------------

MainActivity.java

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    DrawerFragment dfragment = new DrawerFragment();
    ft.add(R.id.drawer, dfragment);
    ft.commit();//追加の反映

  }

}

-------------------------------------------------------------------------------------------------------------

MainFragment.java

public class MainFragment extends Fragment

            implements View.OnClickListener,

            View.OnFocusChangeListener {

  @Override
  public View onCreateView(LayoutInflater inflater,

              ViewGroup container,

              Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
  }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //戻るボタン(onOptionsItemSelected())
    actionBar.setDisplayHomeAsUpEnabled(true);
  }
}

-------------------------------------------------------------------------------------------------------------

DrawerFragment.java

public class DrawerFragment extends Fragment

     mplements AdapterView.OnItemClickListener {

  ActionBarDrawerToggle drawerToggle;
  @Override
  public View onCreateView(LayoutInflater inflater,

              ViewGroup container,

              Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_drawer, container, false);
  }
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {

    //ドロワートグル作成
    this.makeDrawerToggle();

    //FragmentではこれをセットしないとonCreateOptionsMenuが呼ばれない

    setHasOptionsMenu(true);
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
  }
  @Override
  public void onDetach() {
    super.onDetach();
  }
  private void makeDrawer() {
    ArrayList<RSSSite> list = new ArrayList<RSSSite>();
    String sitename = {
       "Google Japan Blog",
       "Google Cloud Platform Japan",
       "Android Developers Blog",
       "はてな"};
    String
siteurls = {
       "http://feeds.feedburner.com/GoogleJapanBlog",
       "http://feeds.feedburner.com/GoogleCloudPlatformJapanBlog",
       "http://feeds.feedburner.com/blogspot/hsDu",
       "http://b.hatena.ne.jp/t/google?mode=rss&sort=hot&threshold=5"};
    int con = sitename.length;
    for (int i = 0; i < con; i++) {
      RSSSite es = new RSSSite();
      es.setSitename(sitename[i]);
      es.setSiteurl(siteurls[i]);
      list.add(es);
    }
    DrawerAdapter adapter = new DrawerAdapter(getActivity(), 0, list);
    adapter.setEnglishSiteList(list);

    Fragment fragment = getFragmentManager().findFragmentById(R.id.drawer);
    ListView lv = (ListView)fragment.getView().findViewById(R.id.drawerlistview);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);//クリックリスナー
  }
  private void makeDrawerToggle() {
    DrawerLayout drawerLayout =

       (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);

    //ドロワーの実装
    //「開ききった」「閉じきった」「スライド中」
    //「ナビゲーションドロワーの状態が変わった」のイベントを受けとれる
    //ドロワーアイコンの設定やアニメーションの設定など

     //onConfigurationChangedやonOptionsItemSelectedのイベントを

    //渡してあげる必要がある
    //第1引数 Activityのインスタンス
    //第2引数 DrawerLayoutのインスタンス
    //第3引数 ドロワー用のアイコン(省略可)
    //第4引数 open時のアクセシビリティ
    //第5引数 close時のアクセシビリティ
    drawerToggle = new ActionBarDrawerToggle(
         getActivity(),
         drawerLayout,
         R.string.drawer_open,
         R.string.drawer_close) {
       @Override
       public void onDrawerOpened(View drawerView) {
         super.onDrawerOpened(drawerView);
         if (!isAdded()) {
          return;
         }

         //onCreateOptionsMenuの呼び出し後にmenuを更新したい場合
         getActivity().invalidateOptionsMenu();
       }
       @Override
       public void onDrawerClosed(View drawerView) {
         super.onDrawerClosed(drawerView);
         if (!isAdded()) {
           return;
         }

         //onCreateOptionsMenuの呼び出し後にmenuを更新したい場合
         getActivity().invalidateOptionsMenu();
       }
    };
    //メインスレッド以外からのUI更新(View.post)
    drawerLayout.post(new Runnable() {
      @Override
      public void run() {

        //ActivityとActionBarDrawerToggleの状態を同期
        drawerToggle.syncState();
      }
    });
    //作成したActionBarDrawerToggleを設置
    drawerLayout.addDrawerListener(drawerToggle);
  }
  public boolean isDrawerOpen() {
    View drawer = getActivity().findViewById(R.id.drawer);
    DrawerLayout mDrawerLayout =

      (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(drawer);
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    //ボタンが押された時ドロワーの開閉をさせる
    if (drawerToggle.onOptionsItemSelected(item)) {

      //trueでリターンしないと他のonOptionsItemSelectedが動いてしまう
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  DrawerLayout drawerLayout =

    (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
    if (drawerLayout != null && isDrawerOpen()) {//ドロワーを開設した場合
      this.makeDrawer();//ドロワー作成
    }
    super.onCreateOptionsMenu(menu, inflater);
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
  }
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    RSSSite es = (RSSSite)parent.getItemAtPosition(position);
    String sitename = es.getSitename();
    String siteurl = es.getSiteurl();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.container, MainFragment.newInstance(position, sitename,     siteurl));//Singletonパターン(newInstance)
    ft.commit();//変更の反映

    DrawerLayout drawerLayout =

       (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawers();//ドロワーを閉じる
  }
}

-------------------------------------------------------------------------------------------------------------

RSSSite.java

public class RSSSite {

  String sitename;
  String siteurl;

  public String getSitename() {
    return sitename;
  }
  public String getSiteurl() {
    return siteurl;
  }
  public void setSitename(String sitename) {
    this.sitename = sitename;
  }
  public void setSiteurl(String siteurl) {
    this.siteurl = siteurl;
  }
}

 

 

 

広告を非表示にする