簡體   English   中英

如何通過多個活動共享導航抽屜

[英]How to share navigation drawer through multiple activities

我的導航抽屜有問題,我想在所有活動中分享它,但是我不知道該怎么做。

如果您有任何想法,它將對我有一定幫助。

這是我的代碼:

主要活動:

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();
}





}

歡迎上課:

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);
  }
}

謝謝。

制作一個具有所有活動抽屜內容的BaseActivty並將該活動擴展到所有其他活動!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM