0
I am working on an app that presents all the games of the Brazilian championship.
I want each tab to show the games of a round, example:
tab1 (wheelset 1)
tab38 (wheel 38)
For this purpose I created tabs using Sherlock.
The games come from the Database I created in Sqlite.
My doubt is how to present the rounds on each tab?
I already have the class that looks for the comic dice and plays them on a list and then Adapter takes care of popular a Listview that is inside the tabs.
How to place a different round on each TAB?
Follow my main codes: Mainactivity.java
public class MainActivity extends SherlockFragmentActivity {
private ActionBar actionBar;
private ViewPager viewPager;
private int rodadas=38;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar= getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),preparaTab(1)));
addActionBarTabs();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_table:
Toast.makeText(this, "Tabela", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
private List<Jogo> preparaTab(int rodada) {
// TODO Auto-generated method stub
BD bd = new BD(this);
return bd.buscar(rodada);
}
private ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionBar.setSelectedNavigationItem(position);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),preparaTab(position+1)));
}
};
private void addActionBarTabs() {
//actionBar = getSupportActionBar();
for (int i=0;i<rodadas;i++) {
ActionBar.Tab tab = actionBar.newTab().setText("Rodada "+(i+1))
.setTabListener(tabListener);
actionBar.addTab(tab);
}
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
private ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
}
Viewpageradapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final int PAGES = 9;
private List<Jogo> jogoList;
public ViewPagerAdapter(FragmentManager fm, List<Jogo> jogoList) {
super(fm);
this.jogoList = jogoList;
}
@Override
public Fragment getItem(int position) {
return new TabFragment(jogoList);
}
@Override
public int getCount() {
return PAGES;
}
}
The way this code ended the performance, because every time I slide to the next TAB the application hangs as it is made a query to popular the listview of the next tab.
You need to pass a list already with the games already selected for each TAB.
– Jorge B.
In theory I would return all the rounds at once and already mount the listviews and Fragments before the application finalizes the boot? Can you give me an example?
– caezar