0

I am trying to convert all my code from activities to fragments in order to use a navigation drawer and eventualy some sliding tabs.

Currently I have a semi working navigation drawer that opens and displays a list. BUt I am new to fragments and am confused because it seems to reload my first fragment everytime I close the navigation drawer and do not select anything.

MainDrawer.java :

package com.beerportfolio.beerportfoliopro;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by Mike on 1/3/14.
 */
public class MainDraw extends FragmentActivity {
    final String[] data ={"Statistics","two","three"};
    final String[] fragments ={
            "com.beerportfolio.beerportfoliopro.StatisticsPage",
            "com.beerportfolio.beerportfoliopro.FragmentTwo",
            "com.beerportfolio.beerportfoliopro.FragmentThree"};
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        //todo: load statistics fragment

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

        final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);


        final ListView navList = (ListView) findViewById(R.id.drawer);
        navList.setAdapter(adapter);

        navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                    @Override
                    public void onDrawerClosed(View drawerView){
                        super.onDrawerClosed(drawerView);
                        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                        tx.replace(R.id.main, Fragment.instantiate(MainDraw.this, fragments[pos]));
                        tx.commit();
                    }
                });
                drawer.closeDrawer(navList);
            }
        });

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.main,Fragment.instantiate(MainDraw.this, fragments[0]));
        tx.commit();

    }


}

StatisticsPage.java :

package com.beerportfolio.beerportfoliopro;

import android.app.ActionBar;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by Mike on 1/3/14.
 */
public class StatisticsPage extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);
        String url = "http://www.beerportfolio.com/app_getStatistics.php?";
        String userURLComp = "u=" + userID;


        url = url + userURLComp ;

        Log.d("basicStats", url);


        new getBasicStats(getActivity()).execute(url);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.statistics_pagelayout, container, false);

    }





}
Mike
  • 6,751
  • 23
  • 75
  • 132

1 Answers1

0

The issue is that your DrawerListener is set in your OnClickListener when it shouldn't be:

@Override public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
    drawer.setDrawerListener(
        new DrawerLayout.SimpleDrawerListener(){
            @Override public void onDrawerClosed(View drawerView){...}
        }
    );
 }

The scope of your pos integer is such that, every time the drawer closes, onDrawerClose thinks that the value of pos hasn't changed. Since there isn't a condition checking that an item was clicked, then the same Fragment is recreated each time the drawer closes (unless you click on a different item).

Set a member boolean to true when an item is clicked and set a member int to its position. Use the boolean to differentiate between an item click and a drawer close:

private boolean mNavItemClicked = false;
private int mNavPosition = 0;
...
@Override protected void onCreate(Bundle savedInstanceState) {
...
    navList.setOnItemClickListener(new ListView.OnItemClickListener {
        @Override public void onItemClick(AdapterView parent, View view, int position, long id) {
            mNavItemClicked = true;
            mNavPosition = position;
            drawerLayout.closeDrawer(navList);
        }
    });
    ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(...) {
        public void onDrawerClosed(View view) {
            if (mNavItemClicked) {
                getSupportFragmentManager().beginTransaction()
                    .replace(R.id.main, Fragment.instantiate(MainDraw.this
                             , fragments[mNavPosition])
                    .commit();

            }
            mNavItemClicked = false;
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

If you decide to use the ActionBarDrawerToggle then follow its design guidelines and call through to the Activity callback methods onConfigurationChanged and onOptionsItemSelected.

iamreptar
  • 1,461
  • 16
  • 29
  • what "sort" of errors? The main idea is that you're setting the drawer listener outside of the onclick listener and that you're using the boolean and int to check for clicks and remember the position of the clicked item. – iamreptar Jan 06 '14 at 18:32