android actionbar viewpager 实现类微信主界面布局

android actionbar viewpager 实现类微信主界面布局  

1 Activity

public class MainActivity extends FragmentActivity {

    private ViewPager pager;

    private ActionBar actionBar;

    private Menu menu;

    private Class<?>[] classes;

    private String[] titles;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        classes = new Class<?>[] { MyAppFragment.class,

                AppCenterFragment.class, ToDoFragment.class };



        titles = new String[] { "我的应用", "应用中心", "待办" };



        pager = (ViewPager) findViewById(R.id.pager);

        pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

        pager.setOnPageChangeListener(new OnPageChangeListener() {



            @Override

            public void onPageSelected(int position) {

                // viewpager 翻页时选中相应的 tab

                actionBar.setSelectedNavigationItem(position);

            }



            @Override

            public void onPageScrolled(int position, float arg1, int arg2) {



            }



            @Override

            public void onPageScrollStateChanged(int position) {

            }

        });



        actionBar = getActionBar();

        actionBar.setBackgroundDrawable(new ColorDrawable(Color.GRAY));

        // actionBar.setDisplayHomeAsUpEnabled(true);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override

            public void onTabSelected(Tab tab,

                    android.app.FragmentTransaction ft) {

                // 点击 tab 时选中相应的 viewpager 页面

                pager.setCurrentItem(tab.getPosition());

            }



            @Override

            public void onTabUnselected(Tab tab,

                    android.app.FragmentTransaction ft) {



            }



            @Override

            public void onTabReselected(Tab tab,

                    android.app.FragmentTransaction ft) {



            }

        };



        for (int i = 0; i < titles.length; i++) {

            actionBar.addTab(actionBar.newTab().setText(titles[i])

                    .setTabListener(tabListener));

        }

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.main, menu);

        this.menu = menu;



        return super.onCreateOptionsMenu(menu);

    }



    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

        case R.id.menu_add:

            Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();

            break;

        case R.id.menu_theme:

            Toast.makeText(this, "更换主题", Toast.LENGTH_SHORT).show();

            break;

        case R.id.menu_system:

            Toast.makeText(this, "系统设置", Toast.LENGTH_SHORT).show();

            break;

        case R.id.menu_uninstall:

            Toast.makeText(this, "卸载应用", Toast.LENGTH_SHORT).show();

            break;

        }



        return true;

    }



    @Override

    public boolean onKeyUp(int keyCode, KeyEvent event) {



        if (keyCode == KeyEvent.KEYCODE_MENU) {

            // 按 menu 键时触发一下 button 的 action 事件

            menu.performIdentifierAction(R.id.menu_more,

                    Menu.FLAG_PERFORM_NO_CLOSE);

            return true;

        }



        return super.onKeyUp(keyCode, event);

    }



    class ViewPagerAdapter extends FragmentPagerAdapter {



        public ViewPagerAdapter(FragmentManager fm) {

            super(fm);

        }



        @Override

        public Fragment getItem(int position) {

            Fragment fragment = null;



            try {

                fragment = (Fragment) classes[position].newInstance();

            } catch (InstantiationException e) {

                e.printStackTrace();

            } catch (IllegalAccessException e) {

                e.printStackTrace();

            }



            return fragment;

        }



        @Override

        public int getCount() {

            return classes.length;

        }

    }

}

2 menu 布局文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" >



    <item

        android:id="@+id/menu_more"

        android:icon="@drawable/menu_more"

        android:orderInCategory="100"

        android:showAsAction="ifRoom"

        android:title="">

        <menu>

            <item

                android:id="@+id/menu_theme"

                android:icon="@drawable/ic_launcher"

                android:showAsAction="always|withText"

                android:title="更换主题"/>

            <item

                android:id="@+id/menu_system"

                android:icon="@drawable/ic_launcher"

                android:showAsAction="always|withText"

                android:title="系统设置"/>

            <item

                android:id="@+id/menu_uninstall"

                android:icon="@drawable/ic_launcher"

                android:showAsAction="always|withText"

                android:title="卸载应用"/>

        </menu>

    </item>

    <item

        android:id="@+id/menu_add"

        android:icon="@drawable/menu_add"

        android:orderInCategory="90"

        android:showAsAction="ifRoom"

        android:title="Add">

    </item>



</menu>

 

DEMO 下载

 

 

你可能感兴趣的:(viewpager)