Skip to content

Instantly share code, notes, and snippets.

@mburumaxwell
Last active November 16, 2018 11:58
Show Gist options
  • Save mburumaxwell/5526215 to your computer and use it in GitHub Desktop.
Save mburumaxwell/5526215 to your computer and use it in GitHub Desktop.
Using menudrawer(SimonVT), viewpager and TitlePageIndicator
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/titlepageindicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip" />
<com.tingle.abeltrac.ui.util.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
package com.tingle.abeltrac.ui.util;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.tingle.abeltrac.R;
public class DevicePagerAdapter extends FragmentPagerAdapter {
private final Resources resources;
private Bundle args;
public DevicePagerAdapter(SherlockFragmentActivity activity, Bundle args) {
super(activity);
this.resources = activity.getResources();
this.args = args;
}
@Override
public int getCount() { return 3; }
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Frag1 fragment1 = new Frag1();
fragment1.setArguments(args);
return fragment1;
case 1:
Frag2 fragment2 = new Frag2();
fragment2.setArguments(args);
return fragment2;
case 2:
Frag3 fragment3 = new Frag3();
fragment3.setArguments(args);
return fragment3;
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return resources.getString(R.string.frag1);
case 1:
return resources.getString(R.string.frag2);
case 2:
return resources.getString(R.string.frag3);
default:
return null;
}
}
}
package com.tingle.abeltrac.ui;
import com.tingle.abeltrac.R;
import com.tingle.abeltrac.ui.util.DevicePagerAdapter;
import com.tingle.abeltrac.ui.util.ViewPager;
import com.viewpagerindicator.TitlePageIndicator;
public class ItemsActivity extends BaseSlidingActivity {
private DevicePagerAdapter adapter;
private ViewPager pager;
private TitlePageIndicator indicator;
private AbsDevicesAdapter abListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.viewpager);
indicator = (TitlePageIndicator) findViewById(R.id.titlepageindicator);
adapter = new DevicePagerAdapter(this, null);
pager.setAdapter(adapter);
indicator.setViewPager(pager, 1);
//difference: the page change listener should be set on the indicator and not the viewpager
indicator.setOnPageChangeListener(new MenuOnPageChangeListener(getMenuDrawer()));
}
}
package com.tingle.abeltrac.ui.util;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import net.simonvt.menudrawer.MenuDrawer;
public class MenuOnPageChangeListener implements OnPageChangeListener {
private MenuDrawer mMenuDrawer;
public MenuOnPageChangeListener(MenuDrawer menuDrawer) {
mMenuDrawer = menuDrawer;
}
@Override
public void onPageScrollStateChanged(int state) { }
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
@Override
public void onPageSelected(int position) {
mMenuDrawer.setTouchMode(position == 0 ? MenuDrawer.TOUCH_MODE_FULLSCREEN : MenuDrawer.TOUCH_MODE_BEZEL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment