AndroidDrawerLayout+fragment佈局實現左右側滑

鴨脖發表於2015-03-29
  • 技術要點: android.support.v4.widget.DrawerLayout

    開啟抽屜: DrawerLayout .openDrawer();

    關閉抽屜:DrawerLayout.closeDrawer( );

    為slidingLayout設定一個layout_grative屬性


    中間\ 左側\ 右側 \

    點選first \ 點選second \



    程式碼:

    activity_main.xml

    01.<android.support.v4.widget.DrawerLayout
    03.android:id="@+id/drawer_layout"
    04.android:layout_width="fill_parent"
    05.android:layout_height="fill_parent" >
    06. 
    07.<FrameLayout
    08.android:id="@+id/fragment_layout"
    09.android:layout_width="fill_parent"
    10.android:layout_height="fill_parent" >
    11.</FrameLayout>
    12.<RelativeLayout
    13.android:id="@+id/menu_layout_left"
    14.android:layout_width="300dp"
    15.android:layout_height="match_parent"
    16.android:layout_gravity="left"
    17.android:background="@android:color/holo_red_light">
    18.<ListView
    19.android:id="@+id/menu_listView_l"
    20.android:layout_width="match_parent"
    21.android:layout_height="match_parent" >
    22.</ListView>
    23.</RelativeLayout>
    24.<RelativeLayout
    25.android:id="@+id/menu_layout_right"
    26.android:layout_width="300dp"
    27.android:layout_height="match_parent"
    28.android:layout_gravity="right"
    29.android:background="#ff333333">
    30.<ListView
    31.android:id="@+id/menu_listView_r"
    32.android:layout_width="match_parent"
    33.android:layout_height="match_parent" >
    34.</ListView>
    35.</RelativeLayout>
    36.</android.support.v4.widget.DrawerLayout>


    first.xml

    01.<LinearLayout
    03.android:id="@+id/drawer_layout"
    04.android:layout_width="match_parent"
    05.android:layout_height="match_parent"
    06.android:orientation="vertical" >
    07. 
    08.<TextView
    09.android:id="@+id/textView1"
    10.android:layout_width="wrap_content"
    11.android:layout_height="wrap_content"
    12.android:text="first"
    13.android:textAppearance="?android:attr/textAppearanceLarge" />
    14. 
    15.</LinearLayout>

    second.xml

    01.<LinearLayout
    03.android:id="@+id/drawer_layout"
    04.android:layout_width="match_parent"
    05.android:layout_height="match_parent"
    06.android:orientation="vertical" >
    07. 
    08.<TextView
    09.android:id="@+id/textView1"
    10.android:layout_width="wrap_content"
    11.android:layout_height="wrap_content"
    12.android:text="second"
    13.android:textAppearance="?android:attr/textAppearanceLarge" />
    14. 
    15.</LinearLayout>

    MainActivity.java

    001.package org.busyboy.drawerlayout;
    002. 
    003. 
    004.import com.example.testdrawerlayout.R;
    005. 
    006. 
    007.import android.os.Bundle;
    008.import android.app.Activity;
    009.import android.support.v4.app.Fragment;
    010.import android.support.v4.app.FragmentActivity;
    011.import android.support.v4.app.FragmentTransaction;
    012.import android.support.v4.widget.DrawerLayout;
    013.import android.view.Gravity;
    014.import android.view.View;
    015.import android.widget.AdapterView;
    016.import android.widget.ArrayAdapter;
    017.import android.widget.ListView;
    018.import android.widget.RelativeLayout;
    019.import android.widget.AdapterView.OnItemClickListener;
    020.import android.widget.TextView;
    021.public class MainActivity extends FragmentActivity
    022.{
    023. 
    024.public static final String[] TITLES = { "First""Second" };
    025.private DrawerLayout mDrawer_layout;//DrawerLayout容器
    026.private RelativeLayout mMenu_layout_left;//左邊抽屜
    027.private RelativeLayout mMenu_layout_right;//右邊抽屜
    028. 
    029.@Override
    030.protected void onCreate(Bundle savedInstanceState)
    031.{
    032.super.onCreate(savedInstanceState);
    033.setContentView(R.layout.activity_main);
    034. 
    035.mDrawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
    036.mMenu_layout_left = (RelativeLayout) findViewById(R.id.menu_layout_left);
    037.mMenu_layout_right = (RelativeLayout) findViewById(R.id.menu_layout_right);
    038.ListView menu_listview_l = (ListView) mMenu_layout_left.findViewById(R.id.menu_listView_l);
    039.ListView menu_listview_r = (ListView) mMenu_layout_right.findViewById(R.id.menu_listView_r);
    040. 
    041.menu_listview_l.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, TITLES));
    042.menu_listview_r.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, TITLES));
    043. 
    044.//監聽選單
    045.menu_listview_l.setOnItemClickListener(new DrawerItemClickListenerLeft());
    046.menu_listview_r.setOnItemClickListener(new DrawerItemClickListenerRight());
    047.}
    048./**
    049.* 左側列表點選事件     
    050.* @author busy_boy
    051.*
    052.*/
    053.public class DrawerItemClickListenerLeft implements OnItemClickListener
    054.{
    055.@Override
    056.public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    057.{
    058.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    059.Fragment fragment = null;
    060. 
    061.//根據item點選行號判斷啟用哪個Fragment
    062.switch (position)
    063.{
    064.case 0:
    065.fragment = new FirstFragment();
    066.break;
    067.case 1:
    068.fragment = new SecondFragment();
    069.break;
    070.default:
    071.break;
    072.}
    073.ft.replace(R.id.fragment_layout, fragment);
    074.ft.commit();
    075.mDrawer_layout.closeDrawer(mMenu_layout_left);//關閉mMenu_layout
    076.}
    077. 
    078.}
    079./**
    080.* 右側列表點選事件     
    081.* @author busy_boy
    082.*
    083.*/
    084.private class DrawerItemClickListenerRight implements OnItemClickListener {
    085.@Override
    086.public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    087.{
    088.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    089.Fragment fragment = null;
    090. 
    091.//根據item點選行號判斷啟用哪個Fragment
    092.switch (position)
    093.{
    094.case 0:
    095.fragment = new FirstFragment();
    096.break;
    097.case 1:
    098.fragment = new SecondFragment();
    099.break;
    100.default:
    101.break;
    102.}
    103.ft.replace(R.id.fragment_layout, fragment);
    104.ft.commit();
    105.mDrawer_layout.closeDrawer(mMenu_layout_right);//關閉mMenu_layout
    106.}
    107.}
    108.}

    FirstFragment.java

    01.package org.busyboy.drawerlayout;
    02. 
    03.import com.example.testdrawerlayout.R;
    04. 
    05.import android.os.Bundle;
    06.import android.support.v4.app.Fragment;
    07.import android.view.LayoutInflater;
    08.import android.view.View;
    09.import android.view.ViewGroup;
    10. 
    11.public class FirstFragment extends Fragment {
    12. 
    13.@Override
    14.public View onCreateView(LayoutInflater inflater, ViewGroup container,
    15.Bundle savedInstanceState) {
    16.return inflater.inflate(R.layout.first, null);
    17.}
    18. 
    19.}

    SecondFragment.java

    01.package org.busyboy.drawerlayout;
    02. 
    03.import com.example.testdrawerlayout.R;
    04. 
    05.import android.os.Bundle;
    06.import android.support.v4.app.Fragment;
    07.import android.view.LayoutInflater;
    08.import android.view.View;
    09.import android.view.ViewGroup;
    10. 
    11.public class SecondFragment extends Fragment {
    12. 
    13.@Override
    14.public View onCreateView(LayoutInflater inflater, ViewGroup container,
    15.Bundle savedInstanceState) {
    16.return inflater.inflate(R.layout.second, null);
    17.}
    18. 
    19.}

    android.support.v4.widget.DrawerLayout 官方文件位置:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

相關文章