Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad custom fragments in Navigation-Drawer's drawer and open corresponding fragments in the main layout
    primarykey
    data
    text
    <p>I want to load different fragments in my drawer, depending upon some condition. Say:</p> <pre><code>if(bit =0){ load fragment0(); } else if(bit =1){ load fragment1(); } </code></pre> <p>And each fragment(fragment0 &amp; fragment1) contains its own list of items and buttons/views. Then depending upon the item click in the drawer fragment, I want to load the corresponding fragment in the main layout. Below is the code I'm using:</p> <p>This is the main activity that loads different fragments in the drawer</p> <pre><code>public class LaunchActivity extends FragmentActivity { private DrawerLayout DrawerLayout; //private ListView DrawerList; private FrameLayout DrawerList; private ActionBarDrawerToggle DrawerToggle; @SuppressWarnings("unused") private CharSequence DrawerTitle; private CharSequence Title; private String[] ListTitles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drawerlist_test); Title = DrawerTitle = getTitle(); ListTitles = getResources().getStringArray(R.array.list_array); DrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); //DrawerList = (ListView) findViewById(R.id.left_drawer); DrawerList = (FrameLayout) findViewById(R.id.left_drawer); // initialize drawer list // Also set a custom shadow that overlays the main content when the drawer opens DrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // change frames int bit =0; if(bit==1){ // add fragments to drawer list Fragment newFragment; FragmentTransaction transaction = getFragmentManager().beginTransaction(); newFragment = new testfragment(); transaction.add(R.id.left_drawer, newFragment); transaction.addToBackStack(null); transaction.commit(); } else { // add fragments to drawer list Fragment newFragment; FragmentTransaction transaction = getFragmentManager().beginTransaction(); newFragment = new testfragment1(); transaction.add(R.id.left_drawer, newFragment); transaction.addToBackStack(null); transaction.commit(); } // set up the drawer's list view with items and click listener // DrawerList.setAdapter(new ArrayAdapter&lt;String&gt;(this, R.layout.drawer_itemlist, ListTitles)); // DrawerList.setOnItemClickListener(new DrawerItemClickListener()); // enable ActionBar app icon to behave as action to toggle nav drawer getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon DrawerToggle = new ActionBarDrawerToggle( this, /* host Activity */ DrawerLayout, /* 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) { getActionBar().setTitle(Title); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(Title); // same title for open/close drawer //getActionBar().setTitle(DrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; DrawerLayout.setDrawerListener(DrawerToggle); if (savedInstanceState == null) { // selectItem(0); } } /** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */ @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. DrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggle DrawerToggle.onConfigurationChanged(newConfig); } // option menu - action bar @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } // Called whenever we call invalidateOptionsMenu() @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view boolean drawerOpen = DrawerLayout.isDrawerOpen(DrawerList); menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. if (DrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getActionBar().getTitle()); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } } /* // Nav Drawer List click // The click listener for ListView in the navigation drawer @SuppressWarnings("unused") private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { selectItem(position); } } private void selectItem(int position) { Fragment newFragment; FragmentTransaction transaction = getFragmentManager().beginTransaction(); switch (position) { case 0: newFragment = new f1(); transaction.replace(R.id.content_frame, newFragment); transaction.addToBackStack(null); transaction.commit(); break; case 1: newFragment = new f2(); transaction.replace(R.id.content_frame, newFragment); transaction.addToBackStack(null); transaction.commit(); break; case 2: newFragment = new f3(); transaction.replace(R.id.content_frame, newFragment); transaction.addToBackStack(null); transaction.commit(); break; case 3: newFragment = new f4(); transaction.replace(R.id.content_frame, newFragment); transaction.addToBackStack(null); transaction.commit(); break; } //DrawerList.setItemChecked(position, true); setTitle(ListTitles[position]); DrawerLayout.closeDrawer(DrawerList); } @Override public void setTitle(CharSequence title) { Title = title; getActionBar().setTitle(Title); } */ } </code></pre> <p>Source code for fragment1 :</p> <pre><code>public class testfragment extends Fragment { ListView DrawerList; private String[] ListTitles; private List&lt;String&gt; mDataSourceList = new ArrayList&lt;String&gt;(); private List&lt;FragmentTransaction&gt; mBackStackList = new ArrayList&lt;FragmentTransaction&gt;(); public static Fragment newInstance(Context context) { testfragment f = new testfragment(); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_testfragment, null); return root; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //add data to ListView for(int i=0, count=20; i&lt;count; i++){ mDataSourceList.add("record" + i); } ListView listView = (ListView) getActivity().findViewById(R.id.listView1); listView.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, mDataSourceList)); } public android.app.Fragment commit() { // TODO Auto-generated method stub return null; } } </code></pre> <p>fragment 2 has also the similar code but with a different items in the list. Both the fragments are loading perfectly in the drawer depending upon the bit value. Now when someone clicks on the drawer list item, I want to load the corresponding fragment in the main layout. </p> <p>My question is Where &amp; How I'm supposed to add onclicklistener for the listview so that appropriate fragment is generated in the main content after the drawer closes ? In the corresponding fragment1/2 or the main activity.</p> <p>I also have another approach for doing the same thing using different lists in the man activity only. What I can do is -- Declare two different listviews along with their onclicklistener switch cases in the main activity. Then I can load those listviews using if-else condition in the drawer.</p> <p>What could be the better approach for a flexible, well structured and well designed app.</p> <p>Here is the another part of my question : <a href="https://stackoverflow.com/questions/20175476/load-navigation-drawer-slider-with-dynamic-fragments">Load navigation drawer slider with Dynamic Fragments</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload