viewpager tablayout tab studio example ejemplo activity android tabs fragment scrollable pageviews

tablayout - tabs android studio material design



Estudio de Android: cómo agregar pestañas con ViewPager (1)

Quiero agregar en un fragmento una pestaña con vista de página (desplazable).

public class MyFragment extends Fragment { private FragmentTabHost tabHost; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { tabHost = new FragmentTabHost(getActivity()); tabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent); tabHost.addTab(tabHost.newTabSpec("one").setIndicator("One"), OneFragment.class, null); tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Two"), TwoFragment.class, null); return tabHost; } @Override public void onDestroyView(){ super.onDestroyView(); tabHost=null; } }

Con este diseño

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> </android.support.v4.app.FragmentTabHost>

Intenté varias soluciones, pero no funcionó. Necesito usar fragmento, no fragmentActivity. El código escrito funciona.


Archivo xml usado como este:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:tabGravity="fill" app:tabMode="fixed" app:tabMaxWidth="0dp" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> <!-- View pager to swipe views --> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent"/> </LinearLayout>

y tu archivo java está aquí:

public class MyFragment extends Fragment { private View view; private TabLayout tabLayout; //This is our viewPager private ViewPager viewPager; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.sticker_fragment, container, false); tabLayout = (TabLayout) view.findViewById(R.id.tabLayout); viewPager = (ViewPager) view.findViewById(R.id.pager); Viewpager adapter = new Viewpager(getActivity().getSupportFragmentManager(), getActivity()); //Adding adapter to pager viewPager.setAdapter(adapter); tabLayout.setupWithViewPager(viewPager); 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) { } }); return view; } }

después de eso el adaptador viewpager es así:

public class Viewpager extends FragmentStatePagerAdapter { final int PAGE_COUNT = 2; private String tabTitles[] = new String[]{"Local","Online"}; private Context context; public Viewpager(FragmentManager fm, Context context) { super(fm); this.context = context; } @Override public Fragment getItem(int position) { switch (position){ case 0: OneFragment oneFragment=new OneFragment(); return oneFragment; case 1: TwoFragment twoFragment=new TwoFragment(); return twoFragment; default: return null; } } @Override public int getCount() { return tabTitles.length; } @Override public CharSequence getPageTitle(int position) { return tabTitles[position]; } }