我有主类和更多3个片段类,当我从主调用片段时它不起作用。我如何调用从appCompacAtivity扩展的主类中的Fragment?我尝试过很多东西,但仍面临同样的问题,请帮帮我......以下是我的代码......提前谢谢
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout)findViewById(R.id.tablayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new HomeFragment(),"Welcome");
viewPagerAdapter.addFragments(new DealsFragment(), "Deals");
viewPagerAdapter.addFragments(new MakemydateFragment(), "Make My Day");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
FragmentManager fr = getFragmentManager();
FragmentTransaction ft = fr.beginTransaction();
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} else if (id == R.id.nav_deals) {
// Intent intent = new Intent(getApplicationContext(), DealsFragment.class); // here i want to call fragment class
// startActivity(intent);
// ft.replace(R.id.container, DealsFragment.class);
// ft.commit();
} else if (id == R.id.nav_knowledgezone) {
} else if (id == R.id.nav_makemyday) {
} else if (id == R.id.nav_specials){
} else if (id == R.id.nav_account) {
} else if (id == R.id.nav_settings) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
public class DealsFragment extends Fragment {
private PagerAdapter mPagerAdapter;
public DealsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_deals, container, false);
ViewPager pager = (ViewPager) view.findViewById(R.id.viewPager2);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(getActivity(), ImageOneDeals.class.getName()));
fragments.add(Fragment.instantiate(getActivity(), ImageTwoDeals.class.getName()));
fragments.add(Fragment.instantiate(getActivity(), ImageThreeDeals.class.getName()));
mPagerAdapter = new ImagePagerAdapter(getChildFragmentManager(), fragments);
pager.setAdapter(mPagerAdapter);
return view;
}
}
答案 0 :(得分:0)
如果您想从活动中调用fragment
的任何方法,请执行此操作。
创建fragment
类的对象,然后调用该方法。
RegFragment fragment = (RegFragment) adapter.getFragment(1);
fragment.onRegError(response);