How to create ViewPager tabs in Android (modern way) - android

I sucessfully created my first ViewPager slider. When I went to the creation of the tabs, I've found that this article iws deprecated. I also found that I can use PagerTitleStrip, which worked but its too ugly. I've found a library for customizing it but I needed to learn how to do it the hard way, because I want to understand how Android works. Could somebody help me understanding how it works?

Here is a guide on Google's suggested usage of tabs in Material Design.
In particular, the TabLayout class from the support design libraries might be what you're after.
Its defaults are very natural, like shown below (taken from the Material Design link above).

try this
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
private void setUpViewPager(ViewPager viewPager)
{
Adapter adapter=new Adapter(getSupportFragmentManager());
adapter.addFragment(new About(),"Article 1");
adapter.addFragment(new About(),"Article 2");
adapter.addFragment(new About(),"Article 3");
viewPager.setAdapter(adapter);
}
Refer here:https://coderzpassion.com/working-appbarlayout-like-whatsapp/

Try this :-
Add the PageTabStrip element as a child of your ViewPager
<android.support.v4.view.ViewPager
android:id="#+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/navigation_toolbar">
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab"
style="#style/AppToolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</android.support.v4.view.ViewPager>
Make sure you implement the below method in your Custom Adapter extending FragmentPagerAdapter :
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Title 1";
case 1:
return "Title 2";
default:
return "null";
}
}

Related

Replacing the Fragment in Tablayout Android

I used the tablayout in my project. I have to replace the fragment in the tablayout. The code is like below
Activity Class
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(Fragment1, "One");
adapter.addFragment(Fragment2, "Two ");
adapter.addFragment(Fragment3, "Three");
viewPager.setAdapter(adapter);
}
Interface in Activity
#Override
public void onInput(int input) {
Here how to replace fragment three
}
I think replacing the fragment is not a good idea. So I would like to show you another way to achieve what you want.
viewPager.setCurrentItem(position); you can use this method to change the page. Here position is page-number starting from 0 to total-pages - 1.
Use ViewModal to pass latest data (in your case "input"). So here you can set value from onInput(int input) and in each fragment of ViewPager you can subscribe/ unsubscribe ViewModal to listen changes. In this way you do not need to replace fragment and the page which is on screen will get latest data and can show.

Design Issue - Launching new Fragment from TabLayout

I'm not quite sure if there's a "best way" to tackle the following design issue.
I have a Tablayout with 2 Tabs in my MainActivity. Each Tab is a different Fragment. I go to Tab1 and see Fragment1. I need to launch a new Fragment (1A) from Fragment 1 and am not sure the best way to do it? I was thinking about one of these.
A) Take the Tabs out of my MainActivity and place them in a separate MainFragment, which gets launched with the app. That way when the user launches Fragment 1A, it replaces just the 1 MainFragment with the Tabs.
or
B) Keep the Tabs in the MainActivity and find a way to replace Fragment 1 with Fragment 1A when under Tab1.
Any suggestions would be appreciated. Thank you.
I think you shouldn't do both of points... Frag1 visible under Tab1 should contain all the layout (including initialy hidden) and logic for this view. If you need to show smth new it may be smaller (then popup, dialog etc.) or expand some layout, maybe with some animation (you may still use ViewPager inside Fragment inside ViewPager, disable touch events and manipulate scrolling programmatically...).
When Action picked by user is intended to show smth so-much-improtant that previous screen is not needed at all then you should probably open new Activity
PS. If you insist to replace current "screen" (in fact Activitys content) note that title of Tab1 may not representing what contains Frag1A. It very depends what kind of content you have there. You may consider move TabLayout/RecyclerView to e.g. FrameLayout container and add to it you Frag1A covering whole previous view including Tabs. In current design guidelines you can even find suggested solution for way of showing new fragment - with circular reveal animation
I do not understand very well what you want, but possibly this.
Use FragmentPagerAdapter..
public class TabsPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_TABS = 3;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return Tab1Fragment.newInstance();
case 1:
return Tab2Fragment.newInstance();
default:
return Tab3Fragment.newInstance();
}
}
#Override
public int getCount() {
return NUM_TABS;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0){
return "Tab 1";
}
if (position == 1){
return "Tab 2";
}
return "Tab 3";
}
In your activity...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Tabs
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
TabsPagerAdapter adapter = new TabsPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);
}

Dynamic Tab with Fragment

total novice,but bit familiar with static tabs addition to view pager.But here I stuck in dynamic tabs which I am able to show but not the fragments of those dynamically received tabs.How would I add fragments to those dynamically received tabs from server.Pardon me spelling and could be ambiguous question
Follow this link example and change the following method.
Visit https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for(int i=0;i<NUM_OF_FRAGMENT;i++){
adapter.addFragment(new YourFragment(), "Title"+i);
}
viewPager.setAdapter(adapter);
}
You need only one fragment for creating dynamic tab.

Fragment with SlidingTabLayout

I've got an app with left navigation drawer, switching between different fragments. Inside one of those fragment I want to implement SlidingTabLayout.
So, what I did is I copied the SlidingTabLayout.java and SlidingTabStrip.java from Google's iosched app. I used them inside my fragment:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<mynamespace.widget.SlidingTabLayout
android:id="#+id/home_fragment_tabs"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/home_fragment_pager"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
And then in my Fragment class, inside onCreateView method, I instantiated my pager and SlidingTabLayout like this:
mPager = (ViewPager) view.findViewById(R.id.home_fragment_pager);
mPager.setAdapter(new HomePagerAdapter(getActivity().getSupportFragmentManager()));
mTabs = (SlidingTabLayout) view.findViewById(R.id.home_fragment_tabs);
mTabs.setViewPager(mPager);
And this is my FragmentPagerAdapter:
class HomePagerAdapter extends FragmentPagerAdapter
{
private String[] tabs = { "Today", "This week", "With star" };
private Fragment[] tabFragments = {
new TodayTabFragment(),
new WeekTabFragment(),
new StarTabFragment()
};
public HomePagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int i)
{
return tabFragments[i];
}
#Override
public int getCount()
{
return 3;
}
#Override
public CharSequence getPageTitle(int position)
{
return tabs[position];
}
}
Also I got separate Fragment class and layout for each tab, which is nothing special. And it works, but as soon as I switch to other fragment with my navigation drawer, when I get back, content of one of tabs just dissapears. Mostly it happens to second tab. Also, if I select the HomeFragment from navigation drawer, when is already displayed, I get a nullpointerexception inside SlidingTabLayout.java at this line: mViewPager.setCurrentItem(i);
Any way of fixing it? I have no idea what to do. I guess my code would work, but inside activity. Possible to make it work inside fragment?
I also faced the same issue. Please use FragmentStatePagerAdapter instead of FragmentPagerAdapter which will solve this issue.
Solution:
What I did is instead of implementing FragmentPagerAdapter, I implemented PagerAdapter then I overriden the instantiateItem(ViewGroup container, int position) method and destroyItem() method.
Link, that helped me: http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.slidingtabsbasic/SlidingTabsBasicFragment.html
Can you try to change constructor of HomePagerAdapter to:
public HomePagerAdapter(SupportFragmentManager fm)
{
super(fm);
}
When you begin transaction instead of suuportfragmanager change it to childfragmentmanager

API 21 Sliding tabs activity

I want to have two tabs to navigate between Fragments, I did it with the old method but now with API 21 all methods are deprecated. I searched a lot but I didn't found any tutorial or sample. I try the SlidingTabs sample from Google but it's not built for Eclipse so I don't know how to integrate it in my app. Can someone help me? Maybe send me a link to a basic tutorial? Thank you!
Check this out, it may help you , it's a very well done menu for navigate through fragments
Just import those 2 classes : SlidingTabStrip and SlidingTabLayout in your project to have tabs in your application. You can take a look at the sample project to see how it is used.
Very simple to use. First import those 2 classes in your project and in your xml layout(my_layout.xml), add the SlidingTabLayout. I also added a viewPager in case you want to use it with a ViewPager :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context="MyActivity">
<SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_pager" />
</LinearLayout>
In your activity,
public class MyActivity extends ActionBarActivity {
private SlidingTabLayout slidingTabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
viewPager = (ViewPager) findViewById(R.id.my_pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
slidingTabLayout = (SlidingTabLayout)findViewById(R.id.sliding_tabs);
slidingTabLayout.setViewPager(viewPager);
}
}
And then you can define your adapter that will added to the SlidingTabLayout. For instance :
public static class MyAdapter extends FragmentPagerAdapter {
private static final int FRAGMENT_1 = 0;
private static final int FRAGMENT_2 = 1;
private static final int FRAGMENT_3 = 2;
public MyAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i){
case FRAGMENT_1 : return new Fragment1();
case FRAGMENT_2 : return new Fragment2();
case FRAGMENT_3 : return new Fragment3();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case FRAGMENT_1 : return "Fragment 1 Title";
case FRAGMENT_2 : return "Fragment 2 title";
case FRAGMENT_3 : return "Fragment 3 Title";
}
return super.getPageTitle(position);
}
#Override
public int getCount() {
return 3;
}
}
If it is a ViewPager you are talking about and not a NavigationDrawer type of thing, it continues to work well on API-21.
Take a look at IOSched application's code for a "Google Play" like ViewPager indicator.
EDIT
Yes setNavigationMode is deprecated as a method and as a UI/UX pattern.
From the official documentation:
This method was deprecated in API level 21.
Action bar navigation modes are deprecated and not supported by inline toolbar action bars.
Consider using other common navigation patterns instead.
Which links to common navigation patterns.
EDIT2
Fixed iosched application code link.

Categories

Resources