Android DrawerLayout demo(抽屉菜单)

来源:互联网 发布:c语言运算符号意义大全 编辑:程序博客网 时间:2024/05/16 09:32

Android DrawerLayout(抽屉菜单)

DrawerLayout中,第一个子View是显示内容的View,第二个子View是抽屉菜单View,在第二个子View中设置属性layout_gravity="left|right",表示抽屉菜单是从左边还是右边滑出。

至于DrawerLayout跟SlidingMenu有什么区别,可以看我的上一篇博文。

demo下载地址:http://download.csdn.net/download/shenyuanqing/9190447

最终效果动画:


MainActivity

package com.example.shen.drawerlayoutdemo.activity;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import android.view.View;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import com.example.shen.drawerlayoutdemo.R;import com.example.shen.drawerlayoutdemo.adapter.MyFragmentPagerAdapter;import com.example.shen.drawerlayoutdemo.fragment.ContactsFragment;import com.example.shen.drawerlayoutdemo.fragment.DiscoveryFragment;import com.example.shen.drawerlayoutdemo.fragment.MessageFragment;import java.util.ArrayList;public class MainActivity extends FragmentActivity implements View.OnClickListener{    private ViewPager viewPager;    private RadioGroup radioGroup;    private RadioButton rbMessage,rbContacts,rbDiscovery;    private ArrayList<Fragment> fragmentList;    private MyFragmentPagerAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化界面组件        initView();        //初始化ViewPager        initViewPager();    }    private void initView(){        viewPager=(ViewPager)findViewById(R.id.view_pager);        radioGroup=(RadioGroup)findViewById(R.id.radio_group);        rbMessage=(RadioButton)findViewById(R.id.rb_message);        rbContacts=(RadioButton)findViewById(R.id.rb_contacts);        rbDiscovery=(RadioButton)findViewById(R.id.rb_discovery);        rbMessage.setOnClickListener(this);        rbContacts.setOnClickListener(this);        rbDiscovery.setOnClickListener(this);    }    private void initViewPager(){        MessageFragment messageFragment=new MessageFragment();        ContactsFragment contactsFragment=new ContactsFragment();        DiscoveryFragment discoveryFragment=new DiscoveryFragment();        fragmentList=new ArrayList<Fragment>();        fragmentList.add(messageFragment);        fragmentList.add(contactsFragment);        fragmentList.add(discoveryFragment);        adapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList);        //ViewPager设置适配器        viewPager.setAdapter(adapter);        //ViewPager显示第一个Fragment        viewPager.setCurrentItem(0);        //ViewPager页面切换监听        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {            }            @Override            public void onPageSelected(int position) {                switch (position){                    case 0:                        radioGroup.check(R.id.rb_message);                        break;                    case 1:                        radioGroup.check(R.id.rb_contacts);                        break;                    case 2:                        radioGroup.check(R.id.rb_discovery);                        break;                }            }            @Override            public void onPageScrollStateChanged(int state) {            }        });        //菜单中控件点击事件        findViewById(R.id.rl_menu).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this,getString(R.string.setting),Toast.LENGTH_SHORT).show();            }        });    }    @Override    public void onClick(View v) {        switch(v.getId()){            case R.id.rb_message:                viewPager.setCurrentItem(0,false);                break;            case R.id.rb_contacts:                viewPager.setCurrentItem(1,false);                break;            case R.id.rb_discovery:                viewPager.setCurrentItem(2,false);                break;        }    }}
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="match_parent"    android:layout_height="match_parent">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">           <RadioGroup            android:id="@+id/radio_group"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:layout_alignParentBottom="true"            android:background="@color/white">            <RadioButton                android:id="@+id/rb_message"                android:drawableTop="@drawable/rb_message_selector"                android:text="@string/message"                android:checked="true"                style="@style/radio_button_main"/>            <RadioButton                android:id="@+id/rb_contacts"                android:drawableTop="@drawable/rb_contacts_selector"                android:text="@string/contacts"                style="@style/radio_button_main"/>            <RadioButton                android:id="@+id/rb_discovery"                android:drawableTop="@drawable/rb_discovery_selector"                android:text="@string/discovery"                style="@style/radio_button_main"/>        </RadioGroup>        <android.support.v4.view.ViewPager            android:id="@+id/view_pager"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_above="@id/radio_group"            />    </RelativeLayout>    <RelativeLayout        android:id="@+id/rl_menu"        android:layout_width="200dp"        android:layout_height="match_parent"        android:background="@color/blue_light"        android:layout_gravity="left">        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical"            android:layout_centerInParent="true">            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@mipmap/ic_launcher"                android:layout_gravity="center"/>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/setting"                android:layout_gravity="center"/>        </LinearLayout>    </RelativeLayout></android.support.v4.widget.DrawerLayout>



1 0
原创粉丝点击