应用启动后选择默认选项卡

时间:2015-12-03 07:31:50

标签: android tabs

我有一个带有标签活动和三个标签的Android应用程序。 这将在我的Overview.class中创建 在我的OverviewPageAdapter.class中,我得到了每个标签内容的正确类和布局。

我有一个设置活动,用户可以在其中选择默认标签,例如 - > TAB2 我将保存到sharedpref中。 现在我想知道,标签2将显示为开始标签。 我怎么能这样做,当我看到概述活动时,每次都会显示标签2?

Overview.class

CameraPosition cameraPos = new CameraPosition.Builder()
                    .target(ll)
                    .zoom(18).bearing(300).tilt(45).build();
         googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos), null);

            if(racecourseroadmarker!=null)
                racecourseroadmarker.remove();
            racecourseroadmarker = googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(ll.latitude, ll
                            .longitude)).anchor(0.5f, 0.5f).draggable(true)
                    .title(getIntent().getStringExtra("busno")).snippet(location)
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.bus)));

            onMarkerDragStart(racecourseroadmarker);
            onMarkerDrag(racecourseroadmarker);
            racecourseroadmarker.showInfoWindow();

OverviewPageAdapter.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) {
            }


        });
    }
}

OverviewTab1.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;
}
}

OverviewTab2.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;
}
}

OverviewTab3.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_tab1.xml

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_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="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_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="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.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" />

2 个答案:

答案 0 :(得分:3)

您可以查看getTabAt // Set INDEX

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

被修改

 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) {

    }
});

答案 1 :(得分:0)

对我有帮助的解决方案

  @Override
    protected void onResume() {
        super.onResume();
            mTabLayout.getTabAt(1).select();
            // or
            mTabLayout.getTabAt(0).select();
        }