Wednesday 7 August 2013

Drawer Layout Not Displaying as Fragment

Drawer Layout Not Displaying as Fragment

The fragment is not displaying or displaying a blank screen. I wanted to
display a drawer layout in my view pager.
switch(arg0){
/** Android tab is selected */
case 0:
DrawerLayoutFragment androidFragment = new
DrawerLayoutFragment();
data.putInt("current_page", arg0+1);
androidFragment.setArguments(data);
return androidFragment;
DrawerLayoutFragment
public class DrawerLayoutFragment extends Fragment{
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mTitle = mDrawerTitle = getActivity().getTitle();
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.two_fragments,
container, false);
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout)
rootView.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) rootView.findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the
drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//enable ActionBar app icon to behave as action to toggle nav drawer
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
getActivity().getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up'
caret */
R.string.drawer_open, /* "open drawer" description for
accessibility */
R.string.drawer_close /* "close drawer" description for
accessibility */
) {
public void onDrawerClosed(View view) {
getActivity().getActionBar().setTitle(mTitle);
getActivity().invalidateOptionsMenu(); // creates call to
onPrepareOptionsMenu()
Log.d("onDrawerClosed", "inside");
}
public void onDrawerOpened(View drawerView) {
getActivity().getActionBar().setTitle(mDrawerTitle);
getActivity().invalidateOptionsMenu(); // creates call to
onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
//getActivity().selectItem(0);
}
return rootView;
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
selectItem(position);
Log.d("DrawerItemClickListener", "inside");
}
}
private void selectItem(int position) {
Log.d("selectItem", "inside");
// update the main content by replacing fragments
PlanetFragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.add(R.id.content_frame, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void setTitle(CharSequence title) {
mTitle = title;
getActivity().getActionBar().setTitle(mTitle);
}
public static class PlanetFragment extends SherlockFragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet,
container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet =
getResources().getStringArray(R.array.planets_array)[i];
int imageId =
getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView)
rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}
twoFragments XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/twoFragments"
android:baselineAligned="false"
>
<FrameLayout
android:id="@+id/twoFragments_content_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<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>
</FrameLayout>
</LinearLayout>

No comments:

Post a Comment