Android – select the default tag after the app starts

I have an android app with tag activity and three tags. This will be created in my overview.class. In my overviewpageadapter.class, I get the correct class and layout for each tab content

I have a setting activity in which users can select a default tag, such as - > TAB2, which I will save to sharedpref. Now I want to realize that tab 2 will be displayed as the start tab. What should I do? When I see "overview activity", tab 2 will be displayed every time?

Overview class

 public class Overview extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.overview);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);





        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

        tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.Tab1)));
        tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.Tab2)));
        tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.Tab3)));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.page_adapter);
        final OverviewPageAdapter adapter = new OverviewPageAdapter (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

/*** SET TAB 2 as START TAB ****/
viewPager.setCurrentItem(2);
/********/


        viewPager.addOnPagechangelistener(new TabLayout.TabLayoutOnPagechangelistener(tabLayout));
        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) {
            }


        });
    }
}

OverviewPageAdapter.class

public class OverviewPageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public OverviewPageAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            OverviewTab1 tab1 = new OverviewTab1();
            return tab1;
        case 1:
            OverviewTab2 tab2 = new OverviewTab();
            return tab2;
        case 2:
            OverviewTab3 tab3 = new OverviewTab3();
            return tab3;
        default:
            return null;
    }
}

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

Overview tab1.class

public class OverviewTab1 extends Fragment {

private View FragementView;

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

Overview tab2.class

public class OverviewTab2 extends Fragment {

private View FragementView;

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

Overview tab3.class

public class OverviewTab3 extends Fragment {

private View FragementView;

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

overview_ tab1.xml

<RelativeLayout
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">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TAB1"
    android:id="tvNoData"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:alpha="1"
    android:textSize="18sp"
    android:textStyle="bold"
    android:allowUndo="false" />

overview_ tab2.xml

<RelativeLayout
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">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TAB2"
    android:id="tvNoData"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:alpha="1"
    android:textSize="18sp"
    android:textStyle="bold"
    android:allowUndo="false" />

overview_ tab3.xml

<RelativeLayout
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">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TAB3"
    android:id="tvNoData"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:alpha="1"
    android:textSize="18sp"
    android:textStyle="bold"
    android:allowUndo="false" />

overview.xml

<RelativeLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <android.support.design.widget.CoordinatorLayout
        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"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary" />

            <android.support.design.widget.TabLayout
                android:id="tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </android.support.design.widget.AppBarLayout>



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


    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

resolvent:

You can check gettabat / / to set the index

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    TabLayout.Tab tab = tabLayout.getTabAt(0); // Count Starts From 0
    tab.select();

Edited

 viewPager.addOnPagechangelistener(new ViewPager.OnPagechangelistener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
       if(position == 2){  // if you want the third page, for example
           //Your code here
       }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>