简体   繁体   中英

Change ListView element selection of a NavigationDrawer

I have an app with a NavigationDrawer .

I'm loading a Fragment from a Fragment(throught a onClick event of a button), but I can't find how to change the the item selection of the ListView of the NavigationDrawer corresponding to the Fragment.

I tryed to do:

            final View navigationView = inflater.inflate(R.layout.fragment_navigation_drawer,container,false);

            ListView lista = (ListView) navigationView.findViewById(R.id.listMenu);

            lista.setFocusable(true);
            lista.setFocusableInTouchMode(true);
            lista.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            lista.performItemClick(lista, 2, lista.getItemIdAtPosition(2));
            lista.setItemChecked(2,true);
            lista.setSelection(2);
            lista.requestFocus();

...after the FragmentTransaction commit(), but the ListView item selected is not changing.

Is there any way to change the selected item by code?

or

Is there any way to invoke a click in the ListView of NavigationDrawer so onNavigationDrawerItemSelected can be executed?

Thanks!

SOLVED changing accessibility to public of the ListView mDrawerListView at NavigationDrawerFragment Class. The extendend explanation in the answer at bottom

well, i do something like that, i select a item from my listview, and send a string to another activity, and do something depending on the string:

mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerList.setAdapter(new CustomAdapter(this, prgmNameList, prgmImages));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);//element's position
        Intent intent;
        int CodigoPeticion=0;
        String num ="";
        switch(position){ //depending of which position is, do something
            case 0:
                intent = new Intent(Dos.this, MainActivity.class); num="cero"; intent.putExtra("po", num);
                CodigoPeticion=2; startActivityForResult (intent,CodigoPeticion); finish(); break;

            case 1: //Toast.makeText(getApplicationContext(), "UNO", Toast.LENGTH_LONG).show();
                intent = new Intent(Dos.this, Uno.class); num="uno"; intent.putExtra("po", num);
                CodigoPeticion=2; startActivityForResult (intent,CodigoPeticion); finish(); break;

            case 2: //Toast.makeText(getApplicationContext(), "DOS", Toast.LENGTH_LONG).show();
                intent = new Intent(Dos.this, Dos.class); num="dos"; intent.putExtra("po", num);
                CodigoPeticion=2; startActivityForResult (intent,CodigoPeticion); finish(); break;

            default: intent = new Intent(Dos.this, Uno.class); num="default"; intent.putExtra("po", num);
                CodigoPeticion=2; startActivityForResult (intent,CodigoPeticion); finish(); break;
        }
    }
}

do something with the item on the next activity (or the same like mine XD) for example, on the onCreate must be:

Bundle extras = getIntent().getExtras();
    if (extras!= null) {
        num =extras.getString("po"); //get the string DrawerItemClickListener
    }

    if(num.equals("cero")){
        Toast.makeText(getBaseContext(), "0", Toast.LENGTH_LONG).show(); nombre="Digimon";
    }else if(num.equals("uno")){
        Toast.makeText(getBaseContext(), "1", Toast.LENGTH_LONG).show(); nombre="Kaminomi";
       //Toast.makeText(getBaseContext(), Nombres, Toast.LENGTH_LONG).show();
    }else if(num.equals("dos")){
        Toast.makeText(getBaseContext(), "2", Toast.LENGTH_LONG).show(); nombre="nisekoi";
    }else {
        Toast.makeText(getBaseContext(), "default "+num, Toast.LENGTH_LONG).show(); testme.setEnabled(false);
    }

hope this help, see ya!

Thanks to all. The examples are enriching.

However, my intention is to highlight an item of NavigationDrawer, without using the finger ;p

I changed the main frame (another fragment) at a button (not clicking the item), but there is still highlighted the previous item, when its showing again the navigationdrawer.

Therefore I need to highlight the correct item by code... if it is possible, or maybe there is a way to invoke a click on a NavigationDrawer, I don't known.

SOLVED

Finally, I found the way for select an item of the NavigationDrawer by code.

The key is at the class NavigationDrawerFragment. There is the ListView mDrawerListView that is defined as private. It only needs to change the accessibility to public.

Then, at the personal button ("find", in my case), there are 2 ways:

1. Accessing to the ListView of the NavigationDrawer and execute the procedure setItemChecked(int position, boolean value):

MainActivity.mNavigationDrawerFragment.mDrawerListView.setItemChecked(1, true);

2. Accessing to the ListView of the NavigationDrawer and perform the click executing the function performItemClick(View view,int position,long id):

 MainActivity.mNavigationDrawerFragment.mDrawerListView.performItemClick(rootView.findViewById(R.id.navigation_drawer),1,0);

Personally, I prefer the 2nd way, because there is code for loading the Fragment at the event NavigationDrawer.onNavigationDrawerItemSelected(int position), so only need click the personal button and call performItemClick

Hope this help another coders ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM