1

I am trying to use ViewPager with ActionBarTabs. I referred to some examples and followed them. but the TabListener and ActionBar.Tab used in the below code are deprecated and i do not know what should i use as alternative?

Please provide an example for the new API

My Code:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

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

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

gradle:

    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.com.vpager_00"
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

    dependencies {
    ccompile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    }
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • `TabLayout` from support and `ViewPager` for sliding tabs – N J Feb 08 '16 at 11:32
  • @NJNileshJ should i download specific library for that?? I am using android studio – Amrmsmb Feb 08 '16 at 11:34
  • I have posted answer please check – N J Feb 08 '16 at 11:40
  • @user2121 - Check my answer, but, there are two things you may want to know, `ccompile` duplicate `c` and try to use the latest supportlibrary.`23.1.1` – ʍѳђઽ૯ท Feb 08 '16 at 12:37
  • @LinX64 ok, but when i used 23.1.1 FragmentStatePagerAdapter class is undefined – Amrmsmb Feb 08 '16 at 12:49
  • So, perhaps you didn't define that and your answer was about: `ActionBar.TabListener` `is deprecated what should use instead` and you should use `Ask question` and paste your codes by creating an another question.maybe, you missed something, btw,, that's the another problem/issue. – ʍѳђઽ૯ท Feb 08 '16 at 12:54

3 Answers3

1

TabLayout official doc

compile dependency

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

include in your layout like

    <RelativeLayout
    android:id="@+id/main_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

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

</RelativeLayout>

see tutorial link

N J
  • 27,217
  • 13
  • 76
  • 96
0

You use use TabLayout and ViewPager instead. (Doc : TabLayout

Basically, add TabLayout view and ViewPager to your main screen.

<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white" />

</LinearLayout>

And create an adapter extends from FragmentPagerAdapter.

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
    private Context context;

    public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

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

    @Override
    public Fragment getItem(int position) {
        return PageFragment.newInstance(position + 1);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

Please see this guide for how to implement the TabLayout with ViewPager.

https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout

Ye Lin Aung
  • 11,234
  • 8
  • 45
  • 51
0

Take a look at my answer:

Android Studio: ActionBar.TabListener does not work with AppCompatActivity?

See: http://developer.android.com/intl/en/reference/android/support/v7/app/ActionBar.TabListener.html

This interface is deprecated. Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead.

And:

Action bar tabs can be replaced by:

Check this link: ActionBarActivity and ActionBar.TabListener is deprecated inside Android Tab Fragment ( Eclipse ApI 22 )

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108