Android Open Source - android-menudrawer View Pager Sample






From Project

Back to project page android-menudrawer.

License

The source code is released under:

Apache License

If you think the Android project android-menudrawer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.simonvt.menudrawer.samples;
/* ww  w.ja v a  2 s.co  m*/
import net.simonvt.menudrawer.MenuDrawer;
import net.simonvt.menudrawer.Position;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class ViewPagerSample extends BaseListSample {

    private PagerAdapter mPagerAdapter;
    private ViewPager mViewPager;
    private int mPagerOffsetPixels;
    private int mPagerPosition;

    @Override
    protected void onCreate(Bundle inState) {
        super.onCreate(inState);

        mMenuDrawer.setContentView(R.layout.activity_viewpagersample);
        mMenuDrawer.setSlideDrawable(R.drawable.ic_drawer);
        mMenuDrawer.setDrawerIndicatorEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }

        mMenuDrawer.setTouchMode(MenuDrawer.TOUCH_MODE_FULLSCREEN);
        mMenuDrawer.setOnInterceptMoveEventListener(new MenuDrawer.OnInterceptMoveEventListener() {
            @Override
            public boolean isViewDraggable(View v, int dx, int x, int y) {
                if (v == mViewPager) {
                    return !(mPagerPosition == 0 && mPagerOffsetPixels == 0) || dx < 0;
                }

                return false;
            }
        });

        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                        mPagerPosition = position;
                        mPagerOffsetPixels = positionOffsetPixels;
                    }
                });

        mPagerAdapter = new PagerAdapter(this);
        mPagerAdapter.addTab(TextViewFragment.class, null);
        mPagerAdapter.addTab(TextViewFragment.class, null);
        mPagerAdapter.addTab(TextViewFragment.class, null);

        mViewPager.setAdapter(mPagerAdapter);
    }

    @Override
    protected void onMenuItemClicked(int position, Item item) {
        mMenuDrawer.closeMenu();
    }

    @Override
    protected int getDragMode() {
        return MenuDrawer.MENU_DRAG_CONTENT;
    }

    @Override
    protected Position getDrawerPosition() {
        return Position.LEFT;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mMenuDrawer.toggleMenu();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        final int drawerState = mMenuDrawer.getDrawerState();
        if (drawerState == MenuDrawer.STATE_OPEN
                || drawerState == MenuDrawer.STATE_OPENING) {
            mMenuDrawer.closeMenu();
            return;
        }

        super.onBackPressed();
    }

    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost. It relies on a
     * trick. Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show. This is not sufficient for switching
     * between pages. So instead we make the content part of the tab host 0dp
     * high (it is not shown) and the TabsAdapter supplies its own dummy view to
     * show as the tab content. It listens to changes in tabs, and takes care of
     * switch to the correct paged in the ViewPager whenever the selected tab
     * changes.
     */
    public static class PagerAdapter extends FragmentPagerAdapter {

        private final Context mContext;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {

            private final Class<?> mClss;
            private final Bundle mArgs;

            TabInfo(Class<?> aClass, Bundle args) {
                mClss = aClass;
                mArgs = args;
            }
        }

        public PagerAdapter(FragmentActivity activity) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.mClss.getName(),
                    info.mArgs);
        }

        public void addTab(Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            mTabs.add(info);
            notifyDataSetChanged();
        }

    }

    public static class TextViewFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            FrameLayout frameLayout = new FrameLayout(getActivity());
            frameLayout.setLayoutParams(new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));

            TextView tv = new TextView(getActivity());
            tv.setText("This is an example of a Fragment in a View Pager");
            frameLayout.addView(tv);
            return frameLayout;
        }
    }
}




Java Source Code List

net.simonvt.menudrawer.BuildLayerFrameLayout.java
net.simonvt.menudrawer.ColorDrawable.java
net.simonvt.menudrawer.DraggableDrawer.java
net.simonvt.menudrawer.FloatScroller.java
net.simonvt.menudrawer.MenuDrawer.java
net.simonvt.menudrawer.NoClickThroughFrameLayout.java
net.simonvt.menudrawer.OverlayDrawer.java
net.simonvt.menudrawer.PeekInterpolator.java
net.simonvt.menudrawer.Position.java
net.simonvt.menudrawer.Scroller.java
net.simonvt.menudrawer.SinusoidalInterpolator.java
net.simonvt.menudrawer.SlideDrawable.java
net.simonvt.menudrawer.SlidingDrawer.java
net.simonvt.menudrawer.SmoothInterpolator.java
net.simonvt.menudrawer.StaticDrawer.java
net.simonvt.menudrawer.ViewHelper.java
net.simonvt.menudrawer.compat.ActionBarHelperCompat.java
net.simonvt.menudrawer.compat.ActionBarHelperNative.java
net.simonvt.menudrawer.compat.ActionBarHelper.java
net.simonvt.menudrawer.samples.ActionBarSherlockSample.java
net.simonvt.menudrawer.samples.BaseListSample.java
net.simonvt.menudrawer.samples.BottomDrawerSample.java
net.simonvt.menudrawer.samples.BottomOverlaySample.java
net.simonvt.menudrawer.samples.Category.java
net.simonvt.menudrawer.samples.FragmentSample.java
net.simonvt.menudrawer.samples.Item.java
net.simonvt.menudrawer.samples.LayoutSample.java
net.simonvt.menudrawer.samples.LeftDrawerSample.java
net.simonvt.menudrawer.samples.LeftOverlaySample.java
net.simonvt.menudrawer.samples.ListActivitySample.java
net.simonvt.menudrawer.samples.MenuAdapter.java
net.simonvt.menudrawer.samples.RightDrawerSample.java
net.simonvt.menudrawer.samples.RightOverlaySample.java
net.simonvt.menudrawer.samples.SamplesActivity.java
net.simonvt.menudrawer.samples.StaticDrawerSample.java
net.simonvt.menudrawer.samples.TopDrawerSample.java
net.simonvt.menudrawer.samples.TopOverlaySample.java
net.simonvt.menudrawer.samples.ViewPagerSample.java
net.simonvt.menudrawer.samples.WindowSample.java