AndroidDrawerLayout+fragment佈局實現左右側滑
-
技術要點: android.support.v4.widget.DrawerLayout
開啟抽屜: DrawerLayout .openDrawer();
關閉抽屜:DrawerLayout.closeDrawer( );
為slidingLayout設定一個layout_grative屬性
中間 左側 右側
點選first 點選second
程式碼:
activity_main.xml
01.
<android.support.v4.widget.DrawerLayout
02.
xmlns:android=
"http://schemas.android.com/apk/res/android"
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
02.
xmlns:android=
"http://schemas.android.com/apk/res/android"
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.xml01.
<LinearLayout
02.
xmlns:android=
"http://schemas.android.com/apk/res/android"
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
相關文章
- android的左右側滑選單實現Android
- Android自定義View(四)側滑佈局AndroidView
- css佈局之左側固定右側自適應佈局CSS
- css佈局,左右固定中間自適應實現CSS
- 實現 UITableViewCell 側滑操作列表UIView
- Activity側滑返回的實現原理
- Android開發筆記(一百二十)兩種側滑佈局Android筆記
- 七種實現左側固定,右側自適應兩欄佈局的方法
- CSS自適應佈局(左右固定 中間自適應或者右側固定 左側自適應)CSS
- Flutter 仿iOS側滑返回案例實現FlutteriOS
- 自定義View:側滑選單實現View
- 自定義RecyclerView實現側滑刪除View
- 如何實現兩欄佈局,右側自適應?三欄佈局中間自適應呢?
- 實現左側固定寬度, 右側自適應的兩欄佈局常見方法
- 微信小程式實現卡片左右滑動效果微信小程式
- ViewPager實現左右無限迴圈滑動Viewpager
- ItemTouchHelper實現可拖拽和側滑的列表
- 自定義View:側滑選單動畫實現View動畫
- MVVM框架下實現左右滑動切換tabMVVM框架
- Android左右滑動效果的程式碼實現Android
- 佈局總結-水平居中佈局的實現
- css3實現側邊滑動選單CSSS3
- div css左右佈局例項程式碼CSS
- 聖盃佈局進階版-flex佈局實現Flex
- Hack 蘋果系統 Api 實現 iOS TableViewCell 側滑方案蘋果APIiOSView
- Flutter 側滑欄及城市選擇UI的實現FlutterUI
- Android側滑返回分析和實現(不高仿微信)Android
- 利用DrawerLayout實現側滑選單學習總結
- CSS佈局–聖盃佈局和雙飛翼佈局以及使用Flex實現聖盃佈局CSSFlex
- CSS多種佈局方式自我實現-水平佈局(二)CSS
- jQuery實現瀑布流佈局jQuery
- Grid 拖拽佈局實現
- css經典佈局之左側固定大小右側自動適應CSS
- 自定義ViewGroup,實現Android的側滑選單ViewAndroid
- css佈局-實現左中右佈局的5種方式CSS
- 使用 CSS columns 佈局來實現自動分組佈局CSS
- UI介面微信底部(ViewPager實現Tab,左右滑動+底部點選)UIViewpager
- ListView 實現帶有Filpper效果的左右滑動刪除 ItemView