天天看點

android fragment側滑,Android DrawerLayout+ fragment 布局實作左右側滑

技術要點:    android.support.v4.widget.DrawerLayout

打開抽屜: DrawerLayout .openDrawer();

關閉抽屜:DrawerLayout.closeDrawer( );

為slidingLayout設定一個layout_grative屬性

中間

android fragment側滑,Android DrawerLayout+ fragment 布局實作左右側滑

  左側

android fragment側滑,Android DrawerLayout+ fragment 布局實作左右側滑

 右側 

android fragment側滑,Android DrawerLayout+ fragment 布局實作左右側滑

點選first 

android fragment側滑,Android DrawerLayout+ fragment 布局實作左右側滑

   點選second 

android fragment側滑,Android DrawerLayout+ fragment 布局實作左右側滑

代碼:

activity_main.xml

first.xml

second.xml

MainActivity.java

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(this, android.R.layout.simple_expandable_list_item_1, TITLES));

menu_listview_r.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, TITLES));

//監聽菜單

menu_listview_l.setOnItemClickListener(new DrawerItemClickListenerLeft());

menu_listview_r.setOnItemClickListener(new DrawerItemClickListenerRight());

}

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

}

}

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

}

}

}

FirstFragment.java

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

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.html