Last active
August 29, 2015 14:06
-
-
Save fnk0/8bac44d164586e13a50b to your computer and use it in GitHub Desktop.
Drawer Layout activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:state_selected="true"> | |
<shape> | |
<solid android:color="@color/drawer_selected" /> | |
</shape> | |
</item> | |
<item android:state_pressed="true"> | |
<shape> | |
<solid android:color="@color/drawer_selected" /> | |
</shape> | |
</item> | |
<item> | |
<shape> | |
<solid android:color="@color/drawer_background" /> | |
</shape> | |
</item> | |
</selector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gabilheri.com.flashcards; | |
import android.app.Activity; | |
import android.content.res.Configuration; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.v4.app.ActionBarDrawerToggle; | |
import android.support.v4.widget.DrawerLayout; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.BaseAdapter; | |
import android.widget.ListView; | |
/** | |
* Created by <a href="mailto:[email protected]">Marcus Gabilheri</a> | |
* | |
* @author Marcus Gabilheri | |
* @version 1.0 | |
* @since 9/7/14. | |
*/ | |
public abstract class DrawerLayoutActivity extends Activity { | |
private final String LOG_TAG = getLogTag(); | |
/** | |
* Nav Drawer stuff | |
*/ | |
private DrawerLayout mDrawerLayout; | |
private ListView mDrawerList; | |
private ActionBarDrawerToggle mDrawerToggle; | |
private CharSequence mDrawerTitle, mTitle; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(getLayout()); | |
/** | |
* Drawer Layout stuff | |
*/ | |
mTitle = mDrawerTitle = getTitle(); | |
mDrawerList = (ListView) findViewById(R.id.list_slidermenu); | |
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); | |
mDrawerList.setOnItemClickListener(new DrawerListener()); | |
mDrawerList.setAdapter(getAdapter()); | |
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, | |
R.drawable.ic_navigation_drawer, // Nav drawer Icon | |
R.string.app_name, // Nav drawer open - description for accessibility | |
R.string.app_name // Nav drawer close | |
) { | |
@Override | |
public void onDrawerOpened(View drawerView) { | |
getActionBar().setTitle(mTitle); | |
invalidateOptionsMenu(); | |
} | |
@Override | |
public void onDrawerClosed(View drawerView) { | |
getActionBar().setTitle(mTitle); | |
invalidateOptionsMenu(); | |
} | |
}; | |
mDrawerLayout.setDrawerListener(mDrawerToggle); | |
init(); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if(mDrawerToggle.onOptionsItemSelected(item)) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void setTitle(CharSequence title) { | |
mTitle = title; | |
getActionBar().setTitle(mTitle); | |
} | |
@Override | |
protected void onPostCreate(Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
// Sync the toggle state after onRestoreInstance has occurred | |
mDrawerToggle.syncState(); | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
// Pass any configuration change to the drawer | |
mDrawerToggle.onConfigurationChanged(newConfig); | |
} | |
/** | |
* Drawer listener. | |
*/ | |
private class DrawerListener implements ListView.OnItemClickListener { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { | |
/** | |
* A handler with a postDelayed is used so it only changes fragments once the drawer is | |
* already closed. This can be adjusted if the timing is not right. | |
* Similar behavior that most Google apps offers. | |
*/ | |
mDrawerLayout.closeDrawer(mDrawerList); | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
displayView(position, null); | |
} | |
}, 300); | |
} | |
} | |
/** | |
* Method to define what clicking on the drawer will do. | |
* | |
* @param position | |
* The position of the clicked item | |
* @param fragmentBundle | |
* A bundle in case something needs to be passed to a specific fragment | |
*/ | |
public abstract void displayView(int position, Bundle fragmentBundle); | |
/** | |
* Any specific initializations should go here. | |
*/ | |
public abstract void init(); | |
/** | |
* Override this method to change the log tag string; | |
* @return | |
*/ | |
private String getLogTag() { | |
return "DrawerActivity"; | |
} | |
/** | |
* Override this method in case of need for a different list colors, etc.. | |
* Shpuld use same Id's to avoid confusion | |
* @return | |
*/ | |
private int getLayout() { | |
return R.layout.drawer_activity; | |
} | |
/** | |
* Getter for the drawer toggle | |
* @return | |
*/ | |
public ActionBarDrawerToggle getDrawerToggle() { | |
return mDrawerToggle; | |
} | |
/** | |
* | |
* @param mDrawerToggle | |
*/ | |
public void setDrawerToggle(ActionBarDrawerToggle mDrawerToggle) { | |
this.mDrawerToggle = mDrawerToggle; | |
} | |
/** | |
* | |
* @return | |
*/ | |
public ListView getDrawerList() { | |
return mDrawerList; | |
} | |
/** | |
* | |
* @return | |
*/ | |
public DrawerLayout getDrawerLayout() { | |
return mDrawerLayout; | |
} | |
/** | |
* | |
* Method to be Overriden that will return an Adapter that extends Base Adapter | |
* The adapter will them be used by the Drawer Layout | |
* | |
* @return | |
*/ | |
protected abstract BaseAdapter getAdapter(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.widget.DrawerLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/drawer_layout" | |
android:layout_height="match_parent" | |
android:layout_width="match_parent" | |
> | |
<!-- Framelayout to display Fragments --> | |
<FrameLayout | |
android:id="@+id/frame_container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
/> | |
<!-- Listview to display slider menu --> | |
<ListView | |
android:id="@+id/list_slidermenu" | |
android:layout_width="240dp" | |
android:layout_height="match_parent" | |
android:layout_gravity="start|bottom" | |
android:choiceMode="singleChoice" | |
android:divider="@color/drawer_background" | |
android:dividerHeight="1dp" | |
android:clipToPadding="false" | |
android:fitsSystemWindows="true" | |
android:listSelector="@drawable/drawer_selector" | |
android:background="@color/drawer_background"/> | |
</android.support.v4.widget.DrawerLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment