If you think the Android project utexas-utilities 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 com.mapsaurus.paneslayout;
//fromwww.java2s.comimport android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.view.View;
import com.actionbarsherlock.view.MenuItem;
import java.lang.ref.WeakReference;
publicclass PhoneDelegate extends ActivityDelegate implements OnBackStackChangedListener {
private SlidingPaneLayout spl;
public PhoneDelegate(PanesActivity a) {
super(a);
}
@Override
publicvoid onCreate(Bundle savedInstanceState) {
if (findViewById(R.id.content_frame) == null)
setContentView(R.layout.phone_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
int menuOffset = getResources().getDimensionPixelSize(R.dimen.menu_offset);
spl = (SlidingPaneLayout) findViewById(R.id.slidingpane_layout);
spl.setShadowResource(R.drawable.shadow_left);
spl.setParallaxDistance(menuOffset);
spl.setCoveredFadeColor(Color.parseColor("#88000000"));
spl.setSliderFadeColor(Color.parseColor("#00000000"));
spl.setPanelSlideListener(new PanelSlideListener() {
@Override
publicvoid onPanelSlide(View panel, float offset) {
}
@Override
publicvoid onPanelOpened(View panel) {
}
@Override
publicvoid onPanelClosed(View panel) {
}
});
FragmentManager fm = getSupportFragmentManager();
fm.addOnBackStackChangedListener(this);
}
/* *********************************************************************
* Interactions with menu/back/etc
* ********************************************************************* */
@Override
publicboolean onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (!spl.isOpen()) {
if (fm.getBackStackEntryCount() > 0) {
return false;
} else {
spl.openPane();
return true;
}
} else {
getActivity().finish();
return true;
}
}
@Override
publicboolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (!spl.isOpen())
spl.openPane();
else
onBackPressed();
return true;
}
return false;
}
@Override
publicvoid onBackStackChanged() {
}
/* *********************************************************************
* Adding, removing, getting fragments
* ********************************************************************* *//**
* Save the menu fragment. The reason to do this is because sometimes when
* we need to retrieve a fragment, that fragment has not yet been added.
*/private WeakReference<Fragment> wMenuFragment = new WeakReference<Fragment>(null);
@Override
publicvoid addFragment(Fragment prevFragment, Fragment newFragment) {
boolean addToBackStack = false;
if (prevFragment == getMenuFragment() || prevFragment == null) {
clearFragments();
} else {
addToBackStack = true;
}
if (spl.isOpen()) spl.closePane();
if (newFragment != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.content_frame, newFragment);
if (addToBackStack) ft.addToBackStack(newFragment.toString());
ft.commit();
}
}
@Override
publicvoid clearFragments() {
FragmentManager fm = getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); i ++)
fm.popBackStack();
}
@Override
publicvoid setMenuFragment(Fragment f) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.menu_frame, f);
ft.commit();
wMenuFragment = new WeakReference<Fragment>(f);
}
@Override
public Fragment getMenuFragment() {
Fragment f = wMenuFragment.get();
if (f == null) {
f = getSupportFragmentManager().findFragmentById(R.id.menu_frame);
wMenuFragment = new WeakReference<Fragment>(f);
}
return f;
}
@Override
public Fragment getTopFragment() {
return getSupportFragmentManager().findFragmentById(R.id.content_frame);
}
@Override
publicvoid showMenu() {
spl.openPane();
}
@Override
publicvoid showContent() {
spl.closePane();
}
}