• AndroidDrawerLayout+fragment布局实现左右侧滑


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

    打开抽屉: DrawerLayout .openDrawer();

    关闭抽屉:DrawerLayout.closeDrawer( );

    为slidingLayout设置一个layout_grative属性

    中间  左侧 右侧

    点击first    点击second

    代码:

    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="fill_parent"
        android:layout_height="fill_parent" >
    
        <FrameLayout
            android:id="@+id/fragment_layout"
            android:layout_width="fill_parent"
       		 android:layout_height="fill_parent" >
        </FrameLayout>
        <RelativeLayout
            android:id="@+id/menu_layout_left"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="@android:color/holo_red_light">
            <ListView
                android:id="@+id/menu_listView_l"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/menu_layout_right"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#ff333333">
            <ListView
                android:id="@+id/menu_listView_r"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>
        </RelativeLayout>
    </android.support.v4.widget.DrawerLayout>
    

    first.xml

    <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" />
      
    </LinearLayout>
    

    second.xml

    <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" />
      
    </LinearLayout>
    

    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<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
            }
       }
    }
    

    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

    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);
    	}
    
    }
    
  • 相关阅读:
    指针型函数与函数型指针 -2021.08.04
    Ubuntu18.04 NAT模式下配置静态IP地址 -2020.11.09
    Linux编译内核 Ubuntu18.04 -2020.11.04
    以PING为例,利用Wireshark深入理解网络层、数据链路层的工作原理 -2020.10.30
    Ubuntu18.04虚拟机的安装
    UNIX/Linux系统中的文件属性
    【计算机四级嵌入式】内存管理
    利用预编译解决C/C++重复定义的错误 -2020.09.13
    使用镜像安装cygwin、gcc并配置CLion IDE -2020.09.12
    Android Studio 4.0.1 找不到R.java 2020.09.08
  • 原文地址:https://www.cnblogs.com/childhooding/p/4377061.html
Copyright © 2020-2023  润新知