AlessioDP AlessioDP - 8 months ago 28
Android Question

ViewPager tabs handling

I have some problems with rendering of pages of a ViewPager with Tabs into the ActionBar.

Example:

Example image



Android info: minimum API 19, compile API 21 (Using AppCompat)



Main layout (main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.MainFragment">

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>






MainFragment:


public class MainFragment extends Fragment {
private ViewPager viewPager;
private PagerTabsAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs;
private int startIn;

MainActivity main;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = (LessonsScheduler) getArguments().get("instance");
tabs = new String[]{"1","2","3"};
}

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

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if(!main.getSupportActionBar().isShowing())
main.getSupportActionBar().show();

viewPager = (ViewPager) main.findViewById(R.id.pager);
actionBar = main.getSupportActionBar();

// Getting item array
mAdapter = new PagerTabsAdapter(getChildFragmentManager(), main.getGroups());
viewPager.setAdapter(mAdapter);

actionBar = main.getSupportActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
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 fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
};
if (actionBar.getTabCount() == 0) {
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab()
.setText(tab_name)
.setTabListener(tabListener));

}
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
}


When i click tab buttons my viewpager doesn't change. I already tested invalidate(), requestLayout(), forceLayout() but these doesn't works.




Line of code to test:


viewPager.setCurrentItem(tab.getPosition());

Answer Source

Currently I'm using TabLayout (thanks @Wizard). It seems to work with old APIs.

I'm still open for new ideas.