onClick Listener on DrawerLayout Does Not Launch T

2019-03-01 06:02发布

I'm attempting to implement a toast depending on the item clicked in a DrawerLayout - however I cannot seem to get the toasts to appear no matter what I do. I'm not sure exactly what I did wrong: any suggestions are greatly appreciated.

    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private String[] drawerListViewItems;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home);
            drawerListViewItems = getResources().getStringArray(R.array.items);
            drawerListView = (ListView) findViewById(R.id.left_drawer);
            drawerListView.setAdapter(new ArrayAdapter<String>(this,
                            R.layout.drawer_listview_item, drawerListViewItems));
            mPager.setOnPageChangeListener(mPageChangeListener);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

            drawerLayout.setDrawerListener(actionBarDrawerToggle);
            drawerListView.setOnItemClickListener(new DrawerItemClickListener());
            drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                            GravityCompat.START); 


    /* The click listener for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

     private void selectItem(int position) {



                if ( position == 1 )
                {
                    Toast.makeText(Home.this, "Item 1", Toast.LENGTH_LONG).show();
                }
                else if ( position == 2 )
                {
                    Toast.makeText(Home.this, "Item 2", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(Home.this, "Item 3", Toast.LENGTH_LONG).show();
                }

                // update selected item and title, then close the drawer
                drawerListView.setItemChecked(position, true);
                drawerLayout.closeDrawer(drawerListView);
            }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
            return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

            if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
                    return true;
            }
            return super.onOptionsItemSelected(item);
    }


            private class DrawerItemClickListener implements
            ListView.OnItemClickListener {

                    @Override
                    public void onItemClick(AdapterView parent, View view,
                                    int position, long id) {

                            Log.d("pk", "onItemClick");
                            // Highlight the selected item, update the title, and close the
                            // drawer
                            // update selected item and title, then close the drawer
                            drawerListView.setItemChecked(position, true);
                            setTitle("......");

                            String text = "menu click... should be implemented";
                            // Toast.makeText(Home.this, text, Toast.LENGTH_LONG).show();
                            drawerLayout.closeDrawer(drawerListView);

                    }

            }

1条回答
▲ chillily
2楼-- · 2019-03-01 06:22

I recently did something very much like this. I was using Fragments but the concept is very much the same.

First I created the DrawerLayout:

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

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

// read in the menu items form the string array
    String[] menuList = getResources().getStringArray(R.array.drawer_menu);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,  R.layout.drawer_list_item, menuList));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

Then I define the DrawerItemClickListener:

/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

Finally I implement the selectItem method:

    private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment;

    if ( position == 1 )
    {
        fragment = new CalendarFrag();
    }
    else if ( position == 2 )
    {
        fragment = new ContactsFrag();
    }
    else
    {
        fragment = new EmailListFragment();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(mDrawerList);
}

You could easily replace the Fragments with Activities, and the beginTransation with a startActivity.

查看更多
登录 后发表回答