起初,我在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();