Navigation drawer Items not registering click event

后端 未结 6 1033
花落未央
花落未央 2020-12-03 07:30

I am struggling to get the Navigation drawer items to register and start and intent for a new activity. The drawer opens fine and is displayed correctly but nothing happens

6条回答
  •  情书的邮戳
    2020-12-03 07:55

    It seems like the your DrawerItemClickListener.class does not work as the only time the selectItem() method is called is at the end of your activities onCreate() at selectItem(0);

    Instead of calling

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    

    you can use this as a parameter and let you Activity implement the ListView.OnItemClickListener itself.

    public class MyActivity extends Activity implements ListView.OnItemClickListener {
    
        public void onCreate(Bundle savedInstanceState) {
            [...]
            mDrawerList = (ListView) findViewById(R.id.left_drawer);
    
            [...]
            mDrawerList.setOnItemClickListener(this);
            [...]
         }
    
        [...]    
    
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
        }
    
        private void selectItem(int position) {
            switch (position) {
                case 1:
                    [...]
                    startActivity(new Intent(this, SplashScreen.class));
                    break;
                [...]
                default:
                break;
            }
            [...]
        }
        [...]
    }
    

提交回复
热议问题