Paul Alexander Paul Alexander - 2 months ago 11
Android Question

Android - Controlling the content of each tab in my tab layout with fragments

I've built an Android app which uses fragments and action bar to create a tab layout.

The app is made up of:


  • MainActivity.java

  • PagerAdapter.java

  • Tab1Fragment.java

  • Tab2Fragment.java

  • activity_main.xml

  • fragment_tab_1.xml

  • fragment_tab_2.xml




    I have this working and I can switch between tabs. My question is how do I control the content of each tab? Obviously I can add my objects to each xml layout but where should I be identifying these objects in my java code? Should I be controlling the content in MainActivity.java or within each separate Fragment.java?



MainActivity.java:

public class MainActivity extends AppCompatActivity {

@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 = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Questions"));
tabLayout.addTab(tabLayout.newTab().setText("Ask a Question"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}





Tab1Fragment.java:

public class Tab1Fragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_1, container, false);


}

}


PagerAdapter.java:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

switch (position) {
case 0:
Tab1Fragment tab1 = new Tab1Fragment();
return tab1;
case 1:
Tab2Fragment tab2 = new Tab2Fragment();
return tab2;

default:
return null;
}
}

@Override
public int getCount() {
return mNumOfTabs;
}
}

Answer

You should reference all of the views within the Fragment code. The views within are defined in that specific Fragment's xml layout file, so the Activity will not be able to reference them easily.

Example:

public class Tab1Fragment extends Fragment {

Button button;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tab_1, container, false);
    button = (Button) view.findViewById(R.id.button);
    //Get all views here

    return view; //Must return a view
}
}
Comments