简体   繁体   English

如何通过多个活动共享导航抽屉

[英]How to share navigation drawer through multiple activities

I've an issue with my navigation drawer, I want to share it through all the activities, but I've no idea of the way to do it. 我的导航抽屉有问题,我想在所有活动中分享它,但是我不知道该怎么做。

If you have any ideas, it will help me for sure. 如果您有任何想法,它将对我有一定帮助。

Here's my code: 这是我的代码:

Main activity: 主要活动:

public class MainActivity extends AppCompatActivity  {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView listView;
private ArrayList<Main_Content> Main_content;
private ListViewAdapter adapter;
ImageButton ConnectButton;
EditText iden_text, pass_text;

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

    Main_content = new ArrayList<>(); //Déclaration de la liste Main_content ou se trouveront les éléments de notre menu

    findViewById();
    setSupportActionBar(toolbar);

    initDrawerLayout();


    ConnectButton.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View view)
        {
            if (iden_text.getText().toString().equals("admin") && pass_text.getText().toString().equals("admin"))
            {
                Toast.makeText(getApplicationContext(), "Vérification des données...", Toast.LENGTH_SHORT).show();
                Intent Bienvenue = new Intent(MainActivity.this,Bienvenue.class);
                Bienvenue.putExtra("iden_text", iden_text.getText().toString());
                startActivity(Bienvenue);

            }
            else
            {
                Toast.makeText(getApplicationContext(), "Identifiants incorrects", Toast.LENGTH_SHORT).show();

            }


        }
    } );
}

public void findViewById()
{
    listView = (ListView) findViewById(R.id.list);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    ConnectButton = (ImageButton) findViewById(R.id.imageButton2);
    iden_text = (EditText) findViewById(R.id.editText);
    pass_text = (EditText) findViewById(R.id.editText2);
}


public void initDrawerLayout() {
    setListViewData();
    setListViewHeader();
    //Mount listview with adapter
    adapter = new ListViewAdapter(this, R.layout.item_listview, Main_content);
    listView.setAdapter(adapter);

    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

public void setListViewHeader() {
    LayoutInflater inflater = getLayoutInflater();
    View header = inflater.inflate(R.layout.header_listview, listView, false);
    listView.addHeaderView(header, null, false);
}

public void setListViewData() { //Liste afficher dans le menu
    Main_content.add(new Main_Content(R.mipmap.id_small,"Carte ID", "Nom Random"));
    Main_content.add(new Main_Content(R.mipmap.note_small, "Relevé de note", "Note"));
    Main_content.add(new Main_Content(R.mipmap.calendar, "Calendrier", "Jours"));
    Main_content.add(new Main_Content(R.mipmap.souper, "Menu Cantine", "Jours"));
    Main_content.add(new Main_Content(R.mipmap.abs, "Absences & Retard", "Jours"));
    Main_content.add(new Main_Content(R.mipmap.disconnect, "Déconnexion", "OUI"));
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);

    return super.onCreateOptionsMenu(menu);
}

/**
 * Replace fragment to Main layout
 * @param Main_content
 */
public void updateMainLayout(Main_Content Main_content) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.container, ContentFragment.newInstance(Main_content));
    transaction.commit();

    //Ferme la navigation une fois que le fragment a été remplacé.
    drawerLayout.closeDrawers();
}





}

Class Welcome: 欢迎上课:

public class Welcome extends  MainActivity {
  TextView tvView;
  private Toolbar toolbar;
  private DrawerLayout drawerLayout;
  private ActionBarDrawerToggle drawerToggle;
  private ListView listView;
  private ArrayList<Main_Content> Main_content;
  private ListViewAdapter adapter;


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

    tvView = (TextView) findViewById(R.id.WelcomeiD);

    Intent Bienvenue = getIntent();

    String Name = Bienvenue.getStringExtra("iden_text");

    tvView.setText("Bienvenue " + Name);
  }
}

Thanks. 谢谢。

制作一个具有所有活动抽屉内容的BaseActivty并将该活动扩展到所有其他活动!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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