OnItemClickListener Navigation Drawer

前端 未结 6 1734
粉色の甜心
粉色の甜心 2020-12-14 12:57

I am making an app from the example of Android Developers with the Navigation Drawer. I made the items but I don\'t know how I can open new Activity from each of items enlis

相关标签:
6条回答
  • 2020-12-14 13:19
    switch(position) {
        case 1:
            Intent a = new Intent(Book.this, SecondActivity.class);
            startActivity(a);
            break;
        case 2:
            Intent b = new Intent(Book.this, ThirdActivity.class);
            startActivity(b);
            break;
        default:
    }
    
    0 讨论(0)
  • 2020-12-14 13:30
     private class DrawerItemClickListener implements ListView.OnItemClickListener
     {
     @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
         {
             SelectItem(position);
         }
      }
    
    0 讨论(0)
  • 2020-12-14 13:33

    Instead of using Activities just use a fragment and A fragment Manager to transition between them

    0 讨论(0)
  • 2020-12-14 13:40

    Instead of your code:

    private void selectItem(int position) {
    
        Fragment fragment = new GalaxyFragment();
        Bundle args = new Bundle();
        args.putInt(GalaxyFragment.ARG_Galaxy_NUMBER, position);
        fragment.setArguments(args);
    
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    
    
        mDrawerList.setItemChecked(position, true);
        setTitle(mGalaxyTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    

    Use this code:

    private void selectItem(int position) {
        switch(position) {
        case 1:
                Intent a = new Intent(MainActivity.this, Activity1.class);
                startActivity(a);
        break;
        case 2:
                Intent b = new Intent(MainActivity.this, Activity2.class);
               startActivity(b);
               break;
        default:
        }
    }
    

    And remove this part: (It is not necessary)

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
    switch(position) {
    case 1:
            Intent a = new Intent(MainActivity.this, Page1.class);
            startActivity(a);
    break;
    case 2:
            Intent b = new Intent(MainActivity.this, Page2.class);
           startActivity(b);
           break;
    default:
    }
    }
    
    0 讨论(0)
  • 2020-12-14 13:42

    In manifest you write this code:

    <activity
        android:name=".firstactivity"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>
    

    In Main activity:

    Intent i=new Intent(MainActivity.this,firstactivity.class);
    startActivity(i);
    
    0 讨论(0)
  • 2020-12-14 13:44

    Use Like that

     switch (position) {
    
                case 0:
                    fragment = new Free();
    
                    break;
                case 1:
                   fragment  = new Manage();
    
                    break;
                case 2:
                  fragment = new Paid();
    
                    break;
                case 3:
                    fragment = new Categories();
    
                    break;
                case 4:
                 logout();
    
                    break;
                default:
                    break;
            }
    
    0 讨论(0)
提交回复
热议问题