GithubHelp home page GithubHelp logo

ogaclejapan / smarttablayout Goto Github PK

View Code? Open in Web Editor NEW
7.1K 191.0 1.3K 14.33 MB

A custom ViewPager title strip which gives continuous feedback to the user when scrolling

License: Apache License 2.0

Java 100.00%
smarttablayout android viewpager

smarttablayout's Introduction

SmartTabLayout

Maven Central Android Arsenal Android Weekly

icon

A custom ViewPager title strip which gives continuous feedback to the user when scrolling.

This library has been added some features and utilities based on android-SlidingTabBasic project of Google Samples.

SmartTabLayout Demo1 SmartTabLayout Demo2 SmartTabLayout Demo3 SmartTabLayout Demo4 SmartTabLayout Demo5 SmartTabLayout Demo6 SmartTabLayout Demo7

Try out the sample application on the Play Store.

Get it on Google Play

Usage

(For a working implementation of this project see the demo/ folder.)

Add the dependency to your build.gradle.

// For androidx (1.0.0)
dependencies {
    compile 'com.ogaclejapan.smarttablayout:library:2.0.0@aar'

    //Optional: see how to use the utility.
    compile 'com.ogaclejapan.smarttablayout:utils-v4:2.0.0@aar'
}

// For legacy android support library (28.0.0)
dependencies {
    compile 'com.ogaclejapan.smarttablayout:library:1.7.0@aar'

    //Optional: see how to use the utility.
    compile 'com.ogaclejapan.smarttablayout:utils-v4:1.7.0@aar'

    //Deprecated since 1.7.0
    compile 'com.ogaclejapan.smarttablayout:utils-v13:1.7.0@aar'
}

Include the SmartTabLayout widget in your layout. This should usually be placed above the ViewPager it represents.

<com.ogaclejapan.smarttablayout.SmartTabLayout
    android:id="@+id/viewpagertab"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:stl_indicatorAlwaysInCenter="false"
    app:stl_indicatorWithoutPadding="false"
    app:stl_indicatorInFront="false"
    app:stl_indicatorInterpolation="smart"
    app:stl_indicatorGravity="bottom"
    app:stl_indicatorColor="#40C4FF"
    app:stl_indicatorThickness="4dp"
    app:stl_indicatorWidth="auto"
    app:stl_indicatorCornerRadius="2dp"
    app:stl_overlineColor="#4D000000"
    app:stl_overlineThickness="0dp"
    app:stl_underlineColor="#4D000000"
    app:stl_underlineThickness="1dp"
    app:stl_dividerColor="#4D000000"
    app:stl_dividerThickness="1dp"
    app:stl_defaultTabBackground="?attr/selectableItemBackground"
    app:stl_defaultTabTextAllCaps="true"
    app:stl_defaultTabTextColor="#FC000000"
    app:stl_defaultTabTextSize="12sp"
    app:stl_defaultTabTextHorizontalPadding="16dp"
    app:stl_defaultTabTextMinWidth="0dp"
    app:stl_distributeEvenly="false"
    app:stl_clickable="true"
    app:stl_titleOffset="24dp"
    app:stl_drawDecorationAfterTab="false"
    />

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/viewpagertab"
    />

In your onCreate method (or onCreateView for a fragment), bind the widget to the ViewPager. (If you use a utility together, you can easily add items to PagerAdapter)

e.g. ViewPager of v4.Fragment

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
        getSupportFragmentManager(), FragmentPagerItems.with(this)
        .add(R.string.titleA, PageFragment.class)
        .add(R.string.titleB, PageFragment.class)
        .create());

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);

SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
viewPagerTab.setViewPager(viewPager);

(Optional) If you use an OnPageChangeListener with your view pager you should set it in the widget rather than on the pager directly.

viewPagerTab.setOnPageChangeListener(mPageChangeListener);

(Optional) Using the FragmentPagerItemAdapter of utility, you will be able to get a position in the Fragment side.

int position = FragmentPagerItem.getPosition(getArguments());

This position will help to implement the parallax scrolling header that contains the ViewPager :P

Attributes

There are several attributes you can set:

attr description
stl_indicatorAlwaysInCenter If set to true, active tab is always displayed in center (Like Newsstand google app), default false
stl_indicatorWithoutPadding If set to true, draw the indicator without padding of tab, default false
stl_indicatorInFront Draw the indicator in front of the underline, default false
stl_indicatorInterpolation Behavior of the indicator: 'linear' or 'smart'
stl_indicatorGravity Drawing position of the indicator: 'bottom' or 'top' or 'center', default 'bottom'
stl_indicatorColor Color of the indicator
stl_indicatorColors Multiple colors of the indicator, can set the color for each tab
stl_indicatorThickness Thickness of the indicator
stl_indicatorWidth Width of the indicator, default 'auto'
stl_indicatorCornerRadius Radius of rounded corner the indicator
stl_overlineColor Color of the top line
stl_overlineThickness Thickness of the top line
stl_underlineColor Color of the bottom line
stl_underlineThickness Thickness of the bottom line
stl_dividerColor Color of the dividers between tabs
stl_dividerColors Multiple colors of the dividers between tabs, can set the color for each tab
stl_dividerThickness Thickness of the divider
stl_defaultTabBackground Background drawable of each tab. In general it set the StateListDrawable
stl_defaultTabTextAllCaps If set to true, all tab titles will be upper case, default true
stl_defaultTabTextColor Text color of the tab that was included by default
stl_defaultTabTextSize Text size of the tab that was included by default
stl_defaultTabTextHorizontalPadding Text layout padding of the tab that was included by default
stl_defaultTabTextMinWidth Minimum width of tab
stl_customTabTextLayoutId Layout ID defined custom tab. If you do not specify a layout, use the default tab
stl_customTabTextViewId Text view ID in a custom tab layout. If you do not define with customTabTextLayoutId, does not work
stl_distributeEvenly If set to true, each tab is given the same weight, default false
stl_clickable If set to false, disable the selection of a tab click, default true
stl_titleOffset If set to 'auto_center', the slide position of the tab in the middle it will keep to the center. If specify a dimension it will be offset from the left edge, default 24dp
stl_drawDecorationAfterTab Draw the decoration(indicator and lines) after drawing of tab, default false

Notes: Both 'stl_indicatorAlwaysInCenter' and 'stl_distributeEvenly' if it is set to true, it will throw UnsupportedOperationException.

How to customize the tab

The customization of tab There are three ways.

  • Customize the attribute
  • SmartTabLayout#setCustomTabView(int layoutResId, int textViewId)
  • SmartTabLayout#setCustomTabView(TabProvider provider)

If set the TabProvider, can build a view for each tab.

public class SmartTabLayout extends HorizontalScrollView {

    //...

    /**
     * Create the custom tabs in the tab layout. Set with
     * {@link #setCustomTabView(com.ogaclejapan.smarttablayout.SmartTabLayout.TabProvider)}
     */
    public interface TabProvider {

        /**
         * @return Return the View of {@code position} for the Tabs
         */
        View createTabView(ViewGroup container, int position, PagerAdapter adapter);

    }

    //...
}

How to use the utility

Utility has two types available to suit the Android support library.

  • utils-v4 library contains the PagerAdapter implementation class for androidx.fragment.app.Fragment
  • (Deprecated) utils-v13 library contains the PagerAdapter implementation class for android.app.Fragment

The two libraries have different Android support libraries that depend, but implemented functionality is the same.

PagerAdapter for View-based Page

ViewPagerItemAdapter adapter = new ViewPagerItemAdapter(ViewPagerItems.with(this)
        .add(R.string.title, R.layout.page)
        .add("title", R.layout.page)
        .create());

viewPager.setAdapter(adapter);

//...

public void onPageSelected(int position) {

  //.instantiateItem() from until .destroyItem() is called it will be able to get the View of page.
  View page = adapter.getPage(position);

}

PagerAdapter for Fragment-based Page

Fragment-based PagerAdapter There are two implementations. Please differences refer to the library documentation for Android.

  • FragmentPagerItemAdapter extends FragmentPagerAdapter
  • FragmentStatePagerItemAdapter extends FragmentStatePagerAdapter
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
        getSupportFragmentManager(), FragmentPagerItems.with(this)
        .add(R.string.title, PageFragment.class),
        .add(R.string.title, WithArgumentsPageFragment.class, new Bundler().putString("key", "value").get()),
        .add("title", PageFragment.class)
        .create());

viewPager.setAdapter(adapter);

//...

public void onPageSelected(int position) {

  //.instantiateItem() from until .destoryItem() is called it will be able to get the Fragment of page.
  Fragment page = adapter.getPage(position);

}

Notes: If using fragment inside a ViewPager, Must be use Fragment#getChildFragmentManager().

Looking for iOS ?

Check WormTabStrip out.

LICENSE

Copyright (C) 2015 ogaclejapan
Copyright (C) 2013 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

smarttablayout's People

Contributors

ataulm avatar cosic avatar ezimetyusup avatar ffts avatar nshmura avatar numan1617 avatar ogaclejapan avatar ralexey avatar satorufujiwara avatar songzhiyong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smarttablayout's Issues

how to change the tab text color when selected

I want to set the selected-unselected textview color to tabs.
Thanx
edit:
I found this way. is this right?

        final SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        viewPagerTab.setViewPager(viewPager);
        viewPagerTab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int count = adapter.getCount();
                for (int i = 0; i < count; i++) {
                    TextView view = (TextView) viewPagerTab.getTabAt(i);
                    view.setTextColor(getResources().getColor(R.color.unselected_tab_text_color));
                }
                TextView view = (TextView) viewPagerTab.getTabAt(position);
                view.setTextColor(getResources().getColor(R.color.selected_tab_text_color));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        TextView view = (TextView) viewPagerTab.getTabAt(0);
        view.setTextColor(getResources().getColor(R.color.selected_tab_text_color)); 

Change Tab highlight

Hi Ogaclejapan,

I am trying to change the color of the tab when I Click on it. It shows blue as default. It is very UGLY

sin titulo

If I want to use pink or transparent as my default highlight, what could I do?, please.

Thanks in advance, and sorry for my lack of knowledge.

Setting a stl_customTabTextLayoutId or stl_defaultTabBackground hides the indicator

Hi
If I set stl_defaultTabBackground to any color, say Blue, it draws on top of the indicator bar.
Same way if I apply a stl_customTabTextLayoutId and set background of textview to blue color, that draws on top of indicator. In all these cases the indicator gets hidden. It looks like these properties are painted after painting the tab indicator which ends up hiding the indicator
Is this a known issue?

Thanks
Vibhor

Don't understand how to attach fragment to activity

I am using this in my activity but activity only shows the tabs but no fragment content...

Activity Class:

public class ActivityUserProfile extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(ActivityUserProfile.this)
                .add(R.string.title_basic, FragmentProfileBasic.class)
                .create());

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);

        SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        viewPagerTab.setViewPager(viewPager);
    }
}

Fragment Class:

public class FragmentProfileBasic extends Fragment {

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

getPage error

Hi!
If you change the position of an element can not be found.

    ..........
    ViewPagerItems.Creator items = ViewPagerItems.with(getActivity());
    for (String type : book_type) {
        items.add(ViewPagerItem.of(type, R.layout.books_list));
    }
    adaptertab = new ViewPagerItemAdapter(items.create());

    viewPager.setAdapter(adaptertab);
    viewPagerTab.setViewPager(viewPager);

Everything works fine until it:

    viewPagerTab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
        @Override
        public void onPageScrollStateChanged(int state) { }

        @Override
        public void onPageSelected(int position) {
            View page = adaptertab.getPage(position);
            RecyclerView cardList = (RecyclerView) page.findViewById(R.id.cardList);   // ERROR
            cardList.setHasFixedSize(true);
            GridLayoutManager llm = new GridLayoutManager(getActivity(), 3);
            cardList.setLayoutManager(llm);
            adapter = new EventsAdapter();
            cardList.setAdapter(adapter);
        }
    });

Why?

Misbehavior after screen rotation

I use this library for swiping between fragments with different layouts (sth like face book app).
After starting the app everything is OK but when I rotate the screen, fragments don't change on swipe however the current fragment containing staggered gridview library (https://github.com/etsy/AndroidStaggeredGrid) is recreated correctly. (number of columns change)
I wonder if it's related to this library or not and can you help me solve this.

Seleted Title color;

hi, this is a great lib, but I found there is no seleted title color in attributes?

Why I can't add a instance of Fragment?

This can be a problem:

add(int title, java.lang.Class<? extends android.support.v4.app.Fragment> clazz)

I need to have a:

add(int title, Fragment fragment)

Why, If I need create a instance of the Fragment, I need use this.

Problem in getPage, return NullPointerException

I get data on api and want to insert them into viewpager

adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(this)
                .add("tab1", fragment_tab_a.getClass())
                .add("tab2", fragment_tab_b.getClass())
                .add("tab3", fragment_tab_c.getClass())
                .create());

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);

        SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        viewPagerTab.setViewPager(viewPager);

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    // answer
                    final String json = "";

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // after answer
                            TextView textView1 = (TextView) adapter.getPage(0).getView().findViewById(R.id.textView1);
                            textView1.setText("1 - ok");
                            TextView textView2 = (TextView) adapter.getPage(1).getView().findViewById(R.id.textView2);
                            textView2.setText("2 - ok");
                            TextView textView3 = (TextView) adapter.getPage(2).getView().findViewById(R.id.textView3);
                            textView3.setText("3 - ok");

                        }
                    });
                } catch (Exception e) {
                    //runOnUiThread(checkInternetConnection);
                    e.printStackTrace();
                }
            }
        };
        thread.start();

but I get the error
java.lang.NullPointerException
on

TextView textView1 = (TextView) adapter.getPage(0).getView().findViewById(R.id.textView1);

please tell me how to do it correctly
project: https://www.dropbox.com/s/nfbo6uwxhlmop6j/SampleTab.zip?dl=0

spacing at before the first tab

I would like to ask if there is any support for adding extra space by custom specification before the first tab horizontally?

Tabs with icons

Hello. How to make tabs with icons? I try this way, but tabs keep showing text titles.

SomeActivity.java

    final LayoutInflater inflater = LayoutInflater.from(viewPagerTab.getContext());
    final Resources res = viewPagerTab.getContext().getResources();

    viewPagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
        @Override
        public View createTabView(ViewGroup container, int position, PagerAdapter 
            ImageView icon = (ImageView) inflater.inflate(R.layout.layout_tab_icon, container, false);
            switch (position) {
                case 0:
                    icon.setImageDrawable(res.getDrawable(R.drawable.ic_group_white_24dp));
                    break;
                case 1:
                    icon.setImageDrawable(res.getDrawable(R.drawable.ic_notifications_white_24dp));
                    break;
                default:
                    throw new IllegalStateException("Invalid position: " + position);
            }
            return icon;
        }
    });

layout_tab_icon.xml

 <?xml version="1.0" encoding="utf-8"?>
 <com.ogaclejapan.smarttablayout.demo.TintableImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:scaleType="center"
    android:background="?attr/selectableItemBackground"
    app:tint="@color/custom_tab_icon"
    />

Inner Vertical TabLayout

What do you think about a inner toolbar tab? This look like the title of Toolbar, but it's a TabLayout, it changes when ViewPager scroll.

how to edit the custom tab child element?

I have custom my own TabView, there have icon, and two textview.
I want to change the textview when I change the viewpage
just like the tab background will change when the viewpage changed.

viewPagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
@OverRide
public View createTabView(ViewGroup container, int position,
PagerAdapter adapter) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(
R.layout.mainactivity_tab, container, false);
ImageView icon = (ImageView) layout
.findViewById(R.id.activity_main_tab_iv_icon);
TextView title = (TextView) layout
.findViewById(R.id.activity_main_tab_tv_title);
TextView notification = (TextView) layout
.findViewById(R.id.activity_main_tab_tv_notification);
switch (position) {
case 0:
icon.setImageDrawable(res
.getDrawable(R.drawable.mainactivity_tab_icon_home));
title.setText(adapter.getPageTitle(position));
notification.setVisibility(View.GONE);
break;
case 1:
icon.setImageDrawable(res
.getDrawable(R.drawable.mainactivity_tab_icon_chart));
title.setText(adapter.getPageTitle(position));
notification.setVisibility(View.GONE);
break;
case 2:
icon.setImageDrawable(res
.getDrawable(R.drawable.mainactivity_tab_icon_message));
title.setText(adapter.getPageTitle(position));
notification.setText("13");
break;
case 3:
icon.setImageDrawable(res
.getDrawable(R.drawable.mainactivity_tab_icon_my));
title.setText(adapter.getPageTitle(position));
notification.setVisibility(View.GONE);
break;
default:
throw new IllegalStateException("Invalid position: "
+ position);
}
return layout;
}
});
viewPagerTab.setViewPager(viewPager);

How to get the Instance of fragment

I want to update the listview of fragement when I click the menuItem in the left DrawerLayout,So I need to get the instance of fragment that new-ed in

adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(this)
                .add(R.string.hot_page, HotPageFragment.class, bundler.get())
                .add(R.string.new_page, NewPageFragment.class,bundler.get())
                .add(R.string.collet_page, CollectPageFragment.class,bundler.get())
                .create());

Always in Center is not in Center

Im implementing the always in center style and i can't get it to be centered. Whats weird is if i rotate landscape then back portrait it is then centered like it should be.

<com.ogaclejapan.smarttablayout.SmartTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:paddingBottom="4dp"
            app:stl_indicatorAlwaysInCenter="true"
            app:stl_indicatorWithoutPadding="false"
            app:stl_indicatorInFront="false"
            app:stl_indicatorInterpolation="smart"
            app:stl_indicatorGravity="bottom"
            app:stl_indicatorColor="@color/accent2"
            app:stl_indicatorThickness="4dp"
            app:stl_indicatorCornerRadius="0dp"
            app:stl_overlineColor="#4D000000"
            app:stl_overlineThickness="0dp"
            app:stl_underlineThickness="1dp"
            app:stl_dividerColor="#00000000"
            app:stl_dividerThickness="1dp"
            app:stl_defaultTabTextAllCaps="true"
            app:stl_defaultTabTextColor="@color/app_tab_text"
            app:stl_underlineColor="@color/transparent"
            app:stl_defaultTabBackground="?attr/selectableItemBackground"
            app:stl_defaultTabTextSize="14sp"
            app:stl_defaultTabTextMinWidth="120dp"
            app:layout_scrollFlags="enterAlways"/>

Issue

java.lang.IllegalStateException: Fragment already added

Hi,

I got this error when I tapped the 5th tab as soon as the UI rendered:

E/AndroidRuntime(28502): java.lang.IllegalStateException: Fragment already added: GenericProductListFragment{3147a7ad #2 id=0x7f0d026f}
E/AndroidRuntime(28502):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1209)
E/AndroidRuntime(28502):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:674)
E/AndroidRuntime(28502):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
E/AndroidRuntime(28502):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
E/AndroidRuntime(28502):    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
E/AndroidRuntime(28502):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1072)
E/AndroidRuntime(28502):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:554)
E/AndroidRuntime(28502):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
E/AndroidRuntime(28502):    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:494)
E/AndroidRuntime(28502):    at com.ogaclejapan.smarttablayout.SmartTabLayout$TabClickListener.onClick(SmartTabLayout.java:474)
E/AndroidRuntime(28502):    at android.view.View.performClick(View.java:4780)
E/AndroidRuntime(28502):    at android.view.View$PerformClick.run(View.java:19866)
E/AndroidRuntime(28502):    at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(28502):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(28502):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(28502):    at android.app.ActivityThread.main(ActivityThread.java:5254)
E/AndroidRuntime(28502):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(28502):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(28502):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
E/AndroidRuntime(28502):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Thanks,
Siddharth

Replace Fragment in view pager

Hi! I have 3 tabs with Fragment1, Fragment2, Fragment3.

I want to be able to replace Fragment2 with Fragment4 dynamically from Fragment2. How can I do this?

When using the stl_indicatorAlwaysInCenter="true", do not scroll position is set in the onCreate

The problem is easily reproducible with the test case.

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

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

  <LinearLayout
    android:id="@+id/header"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary"
    >

    <android.support.v7.widget.Toolbar
      android:id="@id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minHeight="?attr/actionBarSize"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
      >

    </android.support.v7.widget.Toolbar>

    <com.ogaclejapan.smarttablayout.SmartTabLayout
      android:id="@id/viewpagertab"
      android:layout_width="match_parent"
      android:layout_height="@dimen/tab_height"
      android:paddingBottom="4dp"
      app:stl_defaultTabTextColor="@color/white"
      app:stl_defaultTabTextMinWidth="120dp"
      app:stl_indicatorAlwaysInCenter="true"
      app:stl_indicatorColor="@color/accent"
      app:stl_indicatorCornerRadius="1.5dp"
      app:stl_indicatorInterpolation="smart"
      app:stl_indicatorThickness="3dp"
      app:stl_underlineColor="@color/transparent"
      />

  </LinearLayout>

</RelativeLayout>
  protected void onCreate(Bundle savedInstanceState) {

    ....

    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
        getSupportFragmentManager(), pages);

    viewPager.setAdapter(adapter);
    viewPagerTab.setViewPager(viewPager);

    viewPager.setCurrentItem(3);
  }

Problem in `FragmentPagerItems.with().add()` method

The following code -
FragmentPagerItems items = new FragmentPagerItems(this); items.add(FragmentPagerItem.of(getString(R.string.fragment_title_charging), ChargingFragment.class));

returns an error like so -

untitled

Am I missing something?
Here's ChargingFrament.java -

public class ChargingFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    public ChargingFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_charging, container, false);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

Number of tabs on screen

I appreciate your good working solution. Is it possible to set a number of tabs to be forcefully placed on the screen? I have 7 "day-of-week" tabs, and usually only 6 of them on the screen, with tabstrip scrollable. How can I make my app adjust paddings and tab size to make all 7 visible? (and, respectively, for landscape orientation or a very big screen, to limit number of tabs and to stretch only 7)
Thank you

Only Tab Click Listener

Hi sir,

I want to know if there is a way to disable the swipe of the viewpager and let enable only the tabs. I mean, I want the pages change only with a click on the tab. Is this possible?.

The reason I want to use just the click of the tabs is because of this:
picture1

I want to use ViewPager as a parent, and in one of the pages have tabs. I want to know is if this possible? or if you know another way to do.

PD: The page with only tabs loads the tabs dynamically thats why I can't do many static Tabs in XML

As always, thank you for your help.

Have a nice day.

Change tab text

Please tell me
How can I change title of tab?

adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), FragmentPagerItems.with(AnketaActivity.this)
                .add("tab1", AnketaFragment1.class)
                .add("tab2", AnketaFragment2.class)
                .add("tab3", AnketaFragment3.class)
                .create());

I want change "tab1" to "tab1 new"

Question regarding library

Hello ogacleJapan,
I use this library daily and i wanted to know if there was a way to make it compatible with CoordinatorLayout and TabLayout, in order to hide the Toolbar when an item is scrolled in one of the tabs ( a RecyclerView in my case)...

Support for RTL locales

Right now adding android:supportsRtl="true" to the manifest causes STL to behave weirdly. Tab indicator orders and viewpager order is reversed but scroll gesture direction is not.

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapck" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/LightTheme"
        android:supportsRtl="true" >
    .......

STL setup

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(null);
    SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
    setupTabs(viewPagerTab);
    FragmentPagerItems pages = new FragmentPagerItems(this);
    pages.add("a" A.class));
    pages.add("b", B.class));
    pages.add("c", C.class));
    pages.add("d", D.class));
    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(),
            pages);
    viewPager.addOnPageChangeListener(this);
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);
    viewPagerTab.setViewPager(viewPager);

setUpTabs

private void setupTabs(SmartTabLayout layout) {
    final LayoutInflater inflater = LayoutInflater.from(layout.getContext());

    layout.setCustomTabView(new SmartTabLayout.TabProvider() {
        @Override
        public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
            ImageView icon = (ImageView) inflater.inflate(R.layout.custom_tab_icon, container, false);
            switch (position) {
                case 0:
                    icon.setImageResource(R.drawable.a);
                case 1:
                    icon.setImageResource(R.drawable.b);
                case 2:
                    icon.setImageResource(R.drawable.c);
                case 3:
                    icon.setImageResource(R.drawable.d);
                    break;
                default:
                    throw new IllegalStateException("Invalid position: " + position);
            }
            return icon;
        }
    });
}

SmartTabLayout in bottom position

Hi everyone, congratulations for the SmartTabLayout. Helps me a lot.

My question is: Can I put SmartTab in bottom position or only in top of my view pager?

Thanks for everything.
Best Regards, Matheus

Can't click on the tabs, sliding works.

This is what I did:

MainActivity.java

public class MainActivity extends ActionBarActivity {
    // Views injected by Butter Knife.
    @InjectView(R.id.viewpager)
    ViewPager viewPager;

    @InjectView(R.id.viewpagertab)
    SmartTabLayout viewPagerTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        FragmentPagerItems tabs = new FragmentPagerItems(this);
        tabs.add(FragmentPagerItem.of(getString(R.string.tab_one), FragmentOne.class));
        tabs.add(FragmentPagerItem.of(getString(R.string.tab_two), FragmentTwo.class));
        tabs.add(FragmentPagerItem.of(getString(R.string.tab_three), FragmentThree.class));
        tabs.add(FragmentPagerItem.of(getString(R.string.tab_four), FragmentFour.class));

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), tabs);

        viewPager.setAdapter(adapter);

        final LayoutInflater inflater = LayoutInflater.from(viewPagerTab.getContext());
        final Resources res = viewPagerTab.getContext().getResources();

        viewPagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup viewGroup, int position, PagerAdapter adapter) {
                LinearLayout tablayout = (LinearLayout) inflater.inflate(R.layout.layout_tab, viewGroup, false);
                ImageView icon = (ImageView) tablayout.findViewById(R.id.tabicon);
                TextView text = (TextView) tablayout.findViewById(R.id.tabtext);
                switch (position) {
                    case 0:
                        icon.setImageDrawable(res.getDrawable(R.drawable.one));
                        text.setText(R.string.tab_one);
                        break;
                    case 1:
                        icon.setImageDrawable(res.getDrawable(R.drawable.two));
                        text.setText(R.string.tab_two);
                        break;
                    case 2:
                        icon.setImageDrawable(res.getDrawable(R.drawable.three));
                        text.setText(R.string.tab_three);
                        break;
                    case 3:
                        icon.setImageDrawable(res.getDrawable(R.drawable.four));
                        text.setText(R.string.tab_four);
                        break;
                    default:
                        throw new IllegalStateException("Invalid position: " + position);
                }
                return tablayout;
            }
        });

        viewPagerTab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setActionBar(getString(R.string.tab_one), getString(R.string.ab_button_lastpage), getString(R.string.ab_button_refresh));
                        break;
                    case 1:
                        setActionBar(getString(R.string.tab_two), getString(R.string.ab_button_myaccount), true);
                        break;
                    case 2:
                        setActionBar(getString(R.string.tab_three), getString(R.string.ab_button_filter), true);
                        break;
                    case 3:
                        setActionBar(getString(R.string.tab_four));
                        break;
                    default:
                        throw new IllegalStateException("Invalid position: " + position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPagerTab.setViewPager(viewPager);
    }

    private void setActionBar(args) {
        // Sets action bar, not related to tabs.
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.ogaclejapan.smarttablayout.SmartTabLayout
        android:id="@+id/viewpagertab"
        android:layout_width="match_parent"
        android:layout_height="@dimen/tab_height"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        app:stl_distributeEvenly="true"
        app:stl_dividerThickness="0dp"
        app:stl_indicatorInterpolation="smart"
        app:stl_indicatorThickness="0dp"
        app:stl_underlineThickness="0dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

layout_tab.xml

<?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:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/bg_lightgray"
    android:orientation="vertical">

    <TintableImageView
        android:id="@+id/tabicon"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:scaleType="center"
        app:tint="@color/tabicon" />

    <TextView
        android:id="@+id/tabtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:textColor="@color/tabtext"
        android:textSize="12sp" />
</LinearLayout>

Everything runs smoothly, except that switching between fragments can only be achieved by swipe left/right, clicking on the tab bar icons doesn't do anything.

There's an ActionBar on the top of the Activity, and the SmartTabLayout is at the bottom. Between the two displays the Fragment.

Is there something I did wrong? Please help me out :(

New feature request

Hi, your SmartTabLayout is a good library. But it will be very nice if you implement next feature. When user scrolling tabs layout, during scroll viewpager must to be scrolled with the tabs and the tab's indicator will update its state too. Anyway, thank you.

Low FPS with RecyclerView

Hi!
I'm facing some laggy animation on ViewPager transition. Everything on app is fast (animations, recyclerView), but when I start the transition with the library, the fps slow down until the animation ends.
I've test only with a ViewPager and the slide of it is faster than wind. It a prove that is a problem with the library(or animation of smartTab).

<com.ogaclejapan.smarttablayout.SmartTabLayout
                android:id="@+id/smart_tab_layout"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:paddingBottom="4dp"
                android:background="@color/actionbar_color"
                app:stl_indicatorAlwaysInCenter="false"
                app:stl_indicatorInFront="false"
                app:stl_indicatorInterpolation="smart"
                app:stl_indicatorColor="@color/razzmatazz"
                app:stl_indicatorThickness="4dp"
                app:stl_indicatorCornerRadius="2dp"
                app:stl_underlineThickness="0dp"
                app:stl_dividerThickness="0dp"
                app:stl_defaultTabBackground="?attr/selectableItemBackground"
                app:stl_defaultTabTextAllCaps="false"
                app:stl_defaultTabTextColor="@color/mine_shaft"
                app:stl_defaultTabTextSize="12sp"
                app:stl_defaultTabTextHorizontalPadding="12dp"
                app:stl_defaultTabTextMinWidth="0dp"
                app:stl_distributeEvenly="true"/>

Device: S4 mini

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.