Android DrawerLayout demo(抽屉菜单)

Android DrawerLayout(抽屉菜单)

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

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

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

最终效果动画:

Android DrawerLayout demo(抽屉菜单)_第1张图片

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 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();
        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)