Android · 2014年8月5日 0

Android DrawerLayout+ fragment 布局实现左右侧滑

技术要点: android.support.v4.widget.DrawerLayout

打开抽屉: DrawerLayout .openDrawer();

关闭抽屉:DrawerLayout.closeDrawer( );

为slidingLayout设置一个layout_grative属性

 

 

中间\ 左侧\ 右侧 \

点击first \ 点击second \

 

 

代码:

activity_main.xml
<喎�”http://www.2cto.com/kf/ware/vc/” target=”_blank” class=”keylink”>vc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz48L3N0cm9uZz48L3A+CjxwcmUgY2xhc3M9″brush:java;”><frameLayout android:id=”@+id/fragment_layout” android:layout_width=”fill_parent” android:layout_height=”fill_parent” > </frameLayout>

 

 

first.xml

 

1
2
3
4
5
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <textview android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="first" android:textappearance="?android:attr/textAppearanceLarge">
  
</textview></linearlayout>


second.xml

 

 

1
2
3
4
5
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <textview android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second" android:textappearance="?android:attr/textAppearanceLarge">
  
</textview></linearlayout>

 

 

MainActivity.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package org.busyboy.drawerlayout;
import com.example.testdrawerlayout.R;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class MainActivity extends FragmentActivity
{
               
    public static final String[] TITLES = { "First", "Second" };
    private DrawerLayout mDrawer_layout;//DrawerLayout容器
    private RelativeLayout mMenu_layout_left;//左边抽屉
    private RelativeLayout mMenu_layout_right;//右边抽屉
                     
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                         
        mDrawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mMenu_layout_left = (RelativeLayout) findViewById(R.id.menu_layout_left);
        mMenu_layout_right = (RelativeLayout) findViewById(R.id.menu_layout_right);
        ListView menu_listview_l = (ListView) mMenu_layout_left.findViewById(R.id.menu_listView_l);
        ListView menu_listview_r = (ListView) mMenu_layout_right.findViewById(R.id.menu_listView_r);
                         
        menu_listview_l.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_expandable_list_item_1, TITLES));
        menu_listview_r.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_expandable_list_item_1, TITLES));
                         
        //监听菜单
        menu_listview_l.setOnItemClickListener(new DrawerItemClickListenerLeft());
        menu_listview_r.setOnItemClickListener(new DrawerItemClickListenerRight());
    }
    /**
     * 左侧列表点击事件     
     * @author busy_boy
     *
     */
    public class DrawerItemClickListenerLeft implements OnItemClickListener
    {
        @Override
        public void onItemClick(AdapterView<!--?--> parent, View view, int position, long id)
        {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            Fragment fragment = null;
                             
            //根据item点击行号判断启用哪个Fragment
            switch (position)
            {
                case 0:
                    fragment = new FirstFragment();
                    break;
                case 1:
                    fragment = new SecondFragment();
                    break;
                default:
                    break;
            }
            ft.replace(R.id.fragment_layout, fragment);
            ft.commit();
            mDrawer_layout.closeDrawer(mMenu_layout_left);//关闭mMenu_layout
        }
                         
    }
    /**
     * 右侧列表点击事件     
     * @author busy_boy
     *
     */
    private class DrawerItemClickListenerRight implements OnItemClickListener {
     @Override
        public void onItemClick(AdapterView<!--?--> parent, View view, int position, long id)
        {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            Fragment fragment = null;
                             
            //根据item点击行号判断启用哪个Fragment
            switch (position)
            {
                case 0:
                    fragment = new FirstFragment();
                    break;
                case 1:
                    fragment = new SecondFragment();
                    break;
                default:
                    break;
            }
            ft.replace(R.id.fragment_layout, fragment);
            ft.commit();
            mDrawer_layout.closeDrawer(mMenu_layout_right);//关闭mMenu_layout
        }
   }
}
</string></string>


FirstFragment.java

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package org.busyboy.drawerlayout;
import com.example.testdrawerlayout.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.first, null);
    }
}


SecondFragment.java

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package org.busyboy.drawerlayout;
import com.example.testdrawerlayout.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.second, null);
    }
}

 

 

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

 

参考设计文档:

Providing Up Navigation

http://developer.android.com/training/implementing-navigation/ancestral.html

http://developer.android.com/design/patterns/app-structure.html

http://developer.android.com/training/implementing-navigation/nav-drawer.html#ActionBarIcon

Share this: