我在另一个班级时,如何将mainActivity的tablayout内容更改为另一个内容

时间:2019-03-14 13:08:45

标签: android

起初,我在MainActivity类上有两个布局。它们是“ Login class”和“ Help class”。我在MainActivity中包括了这两个类,作为tablayout。现在,我需要在成功登录后将这两个选项卡布局更改为“ home class”和“ Read class”。 这是我的mainActivity

public class MainActivity extends AppCompatActivity  {
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        // now adding fragments to adapter one by one
        adapter.addFragment(new Login(),"Login");
        adapter.addFragment(new Help(),"Help");
        viewPager.setAdapter(adapter);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }
}

在mainActivity中,两个选项卡布局(登录和帮助)均正常工作。


当我进入登录课程时,这是我的问题的代码片段。

    if (txt_id.equals(id))
     {
      if (txt_password.equals(password))
          {
           //What I want is changing the mainActivity tablayout here
             MainActivity mainActivity = new MainActivity();//creating mainActivity object to call the following functions to change the tablayout of main activity
             mainActivity.adapter.addFragment(new Home(),"Home");
             mainActivity.adapter.addFragment(new Read(),"Read");
            mainActivity.viewPager.setAdapter(mainActivity.adapter);
            mainActivity.tabLayout.setupWithViewPager(mainActivity.viewPager);
         }
    else
      Toast.makeText(getContext(), "Password error", Toast.LENGTH_LONG).show();
   }
  else
Toast.makeText(getContext(), "Your Id is incorrect", Toast.LENGTH_LONG).show();

0 个答案:

没有答案