Search code examples
androidactionbarsherlockslidingdrawerslidingmenu

How to use SherlockFragmentActivity with SlidingMenu?


Am using SlidingMenu which uses different types of Fragment/SherlockFragment as menu.
Am in need of SherlockFragmentActivity, as I am using Tabs inside menu.
How to use SherlockFragmentActivity with SlidingMenu?

If it's not possible, is there any other library through which I can have sliding menu & tabs inside sliding menu? Am not sure if it can be achieved using android-menudrawer

Edit : want to achieve the below. ie, Tabs inside Menu
when menu button is clicked, menu Fragment is opened, inside Menufragment I want to add tabs.

This is home screenMenu With Tabs


Solution

  • EDIT:

    I wanted to take this design as a challenge and see what is the result:

    enter image description here enter image description here

    As I suggested in the comments, I used PagerSlidingTabStrip andViewPager.

    I did not include the ActionBarSherlock, but if needed that will be easy to modify: the MainActivity will be required to extend from SherlockFragmentActivity, and the theme @style/Theme.Sherlock.Light added to manifest file, and that is all.

    (Just to be sure you got the idea, PagerSlidingTabStrip is the one who creates the tabs at the bottom)

    Here are the steps I took to integrate PagerSlidingTabStrip and ViewPager with SlidingMenu:

    1 - Import SlidingMenu library into Eclipse workspace
    2 - Import PagerSlidingTabStrip library into Eclipse workspace
    3 - Add Android Support Library to your project (and copy the same .jar into SlidingMenu and PagerSlidingTabString libraries, otherwise eclipse might complain)
    4 - A minimal example of MainActivity:

    public class MainActivity extends FragmentActivity  {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            SlidingMenu menu = new SlidingMenu(this);
            menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
            menu.setBehindOffset(50);
            menu.setFadeDegree(0.35f);
            menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
            menu.setMenu(R.layout.left_menu);
    
            ViewPager pager = (ViewPager) findViewById(R.id.pager);
            pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    
            PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
            tabs.setViewPager(pager);
        }
    }
    

    5 - The layout of sliding menu, R.layout.left_menu:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/left_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#CCC"
        android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res/org.grec">
    
        <com.astuetz.viewpager.extensions.PagerSlidingTabStrip
            android:id="@+id/tabs"
            app:shouldExpand="true"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_height="48dip" />
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/tabs" />
    
    </RelativeLayout>
    

    6 - The ViewPagerAdapter:

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    
        private final int PAGES = 3;
        private String[] titles={"Tab 1", "Tab 2", "Tab 3"};
    
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new TabFragment1();
                case 1:
                    return new TabFragment2();
                case 2:
                    return new TabFragment3();
                default:
                    throw new IllegalArgumentException("The item position should be less or equal to:" + PAGES);
            }
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    
        @Override
        public int getCount() {
            return PAGES;
        }
    }
    

    7 - And an example of fragment, TabFragment1.java (the other 2 are similar):

    public class TabFragment1 extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_tab_1, container, false);
        }
    }
    

    8 - And the layout of the fragment R.layout.fragment_tab_1 which is pretty simple, it contains a single TextView, so I won't include it here.

    I hope this example addresses your issue and will help you getting started on the right track.

    Full source on github: https://github.com/vgrec/SlidingMenuWithViewpager