1

I am making an android application which has a Navigation Drawer in it which is my ListView.

It works fine as I can drag it out and then hit one of the items which will highlighted and swap Fragments. However when I go to swap Fragments pro grammatically, such as by a button in Settings that calls

fragmentUpdate(BBVar.HOME)

the Fragment shown will switch to Home's view but Settings will still be highlighted in the ListView.

I've tired using:

dList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
dList.setSelection(0);

but it doesn't do the job.

I've looked at this but I'm not using a NavigationView so it doesn't help.

How can I set a ListView's item as selected pro grammatically as well as in touch mode?

public class Main extends Activity {
    private String[] menu = new String[] { BBVar.NAV_HOME,  BBVar.NAV_SETTINGS }; // BBVar is just a class containing String variables

    private DrawerLayout dLayout;
    private ListView dList;
    private ArrayAdapter<String> adapter;

    private FragmentManager fManager;
    private Home fHome = new Home(this); // extends Fragment
    private Settings fSettings = new Settings(this);// extends Fragment
    private String curFrag = BBVar.NAV_HOME;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        actionbar();

        dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        dList = (ListView) findViewById(R.id.left_drawer);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, menu);
        fManager = getFragmentManager();

        dList.setBackgroundColor(Color.parseColor("#1c2d4b"));
        dList.setAdapter(adapter);
        dList.setSelector(android.R.color.holo_blue_dark);
        dList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                dLayout.closeDrawers();
                fragmentUpdate(menu[position]);

            }

        });
        fragmentUpdate(BBVar.NAV_HOME);

    }
    private Fragment fragmentSearch(String fragment) {

        switch (fragment) {
        case BBVar.NAV_HOME:
            return fHome;
        case BBVar.NAV_SETTINGS:
            fSettings.previous(curFrag);
            return fSettings;
        default:
            return null;
        }
    }

    public void fragmentUpdate(String fragment) {
        fragmentSwitch(fragmentSearch(fragment));
        curFrag = fragment;
    }

    private void fragmentSwitch(Fragment fragment) {
        fManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    }
}
Community
  • 1
  • 1
Crimson Bloom
  • 287
  • 2
  • 13

0 Answers0