0
I have the following code:
public class ActivityCategorias extends AppCompatActivity implements MaterialTabListener{
private List<Category> categoriesList;
MaterialTabHost tabHost;
ViewPager pager;
ViewPagerAdapter adapter;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
//Lollipop Style
Utils.setStatusBarcolor(getWindow(), getResources().getColor(R.color.primary_dark));
if (Utils.isLollipop())
findViewById(R.id.toolbar).setVisibility(View.GONE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//Get and Set the Post Title
String itemTitle = "Categorias";
setTitle(Html.fromHtml(itemTitle));
tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
pager = (ViewPager) this.findViewById(R.id.pager);
// init view pager
adapter = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
Toast.makeText(ActivityCategorias.this,
"Selected page position: " + position, Toast.LENGTH_SHORT).show();
tabHost.setSelectedNavigationItem(position);
}
});
categoriesList = AppController.getInstance().getPrefManger().getCategories();
for (Category a : categoriesList) {
//retorna a.getId() e a.getTitle()
tabHost.addTab(
tabHost.newTab()
.setText(a.getTitle())
.setTabListener(this)
);
}
}
@Override
public void onTabSelected(MaterialTab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(MaterialTab tab) {
}
@Override
public void onTabUnselected(MaterialTab tab) {
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int position) {
Fragment fragment = null;
fragment = new FragmentMain().newInstance("1234", "Teste");
return fragment;
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
}
}
This part of the code returns an array of categories and creates the menus and returns the ID and category name:
categoriesList = AppController.getInstance().getPrefManger().getCategories();
for (Category a : categoriesList) {
//retorna a.getId() e a.getTitle()
tabHost.addTab(
tabHost.newTab()
.setText(a.getTitle())
.setTabListener(this)
);
}
}
As we can see, the menus are created normally, however, I also want to pass the id to Fragment, ex in this line:
fragment = new FragmentMain().newInstance("1234", "Nome da Categoria");
Part of the Fragmentmain class:
public class FragmentMain extends Fragment {
private static final String TAG = FragmentMain.class.getSimpleName();
public static final String bundleCategoryId = "categoryId";
public static final String bundleCategoryName = "categoryName";
public FragmentMain() {
}
public static FragmentMain newInstance(String categoryId, String categoryName) {
FragmentMain f = new FragmentMain();
Bundle args = new Bundle();
args.putString(bundleCategoryId, categoryId);
args.putString(bundleCategoryName, categoryName);
f.setArguments(args);
return f;
}
I tried several ways and I couldn’t. Is it possible to do this in this code template? Thank you.
Where is the class code
FragmentMain
? It’s also strange the way you’re instilling it, it shouldn’t befragment = FragmentMain().newInstance(...);
without thenew
?– ramaral
Add part of Fragmentmain to express myself better. Thank you.
– anderson
Alter
fragment = new FragmentMain().newInstance("1234", "Nome da Categoria");
forfragment = FragmentMain().newInstance("1234", "Nome da Categoria");
and see if it works.– ramaral
Hello @ramaral, Fragmentmain loads normally, but what I want to do is the following: inside newInstance(...) I want you to load the ID and Category Name to then pass this data to Fragmentmain. This data: "1234", "Name of the Category" I can take normally for Fragmentemain, but the problem is to take this data coming from the TABS. Did you understand?
– anderson