Reputation: 3747
I have implemented a Navigation Drawer
programmatically. While everything else works when I click in the upper left icon the screen darkens (the animation when the drawer indeed slides in) but the drawer does not slide at all.
Here is my code.
public class MainActivity extends Activity {
private static final String TAG = "MAIN_ACTIVITY";
private DrawerLayout mDrawerLayout;
public static ArrayList<String> mOptions=new ArrayList<String>();
// ListView represents Navigation Drawer
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private MainActivityLayout mainActivityLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivityLayout=new MainActivityLayout(this,null);
setContentView(mainActivityLayout);//changed this
mOptions.add("Search");
mOptions.add("Inbox");
mOptions.add("Shout-A-Loud");
mOptions.add("Saved Profiles");
mOptions.add("Profile viewers");// premium feature. Will display profile views but not viewers.
mOptions.add("Settings");
mOptions.add("App Info");
mDrawerLayout=(DrawerLayout) mainActivityLayout;//this changed
mDrawerList=(ListView) new ListViewCustom(this,null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),
R.layout.drawer_list_item, mOptions);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
mTitle="Social Geek";
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
mTitle="Select";
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
if (savedInstanceState == null) {
selectItem(0);
}
}
Second Class
public class MainActivityLayout extends DrawerLayout{
static Context context;
static DrawerLayout DrawerLayoutCustom;
static ListViewCustom ListViewCustom;
public MainActivityLayout(Context context,AttributeSet attr) {
super(context,attr);
MainActivityLayout.context=context;
ListViewCustom=new ListViewCustom(context,null);
LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
setLayoutParams(layoutParams);
addView(ListViewCustom);
initializeEverything(context, null);
}
Third Class
public class ListViewCustom extends ListView{
static final int LISTVIEW_CUSTOM=0;
public ListViewCustom(Context context,AttributeSet attrs) {
super(context,attrs);
DrawerLayout.LayoutParams layoutParams=
new DrawerLayout.LayoutParams(240,LayoutParams.MATCH_PARENT);
layoutParams.gravity=Gravity.START;
setBackgroundColor(111);
setDivider(getResources().getDrawable(android.R.color.transparent));
setChoiceMode(CHOICE_MODE_SINGLE);
setLayoutParams(layoutParams);
}
}
Upvotes: 1
Views: 1332
Reputation: 51411
I strongly recommend creating the custom layout in .xml.
What you are doing wrong is, that you create the DrawerLayout
inside the ListView
, and not even add it to any layout. Meaning, your ListView is the parent, and the DrawerLayout the child, which is incorrect.
The correct approach is that the DrawerLayout
is the parent, and the ListView
the child.
(meaning, the ListView needs to be inside the DrawerLayout).
xml example:
<!--- drawerlayout needed to make it all work -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view this is where you display your stuff -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer, this will slide in and out -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Have a look here as well: How to create a NavigationDrawer
Upvotes: 2