在特定选项卡上单击打开活动

时间:2017-01-30 12:09:18

标签: android android-viewpager android-tablayout

我使用TabLayout和非滑动ViewPager创建了带图标的底部标签。我想要的是在点击特定标签(Activity)时打开position == 2。其他选项卡将按原样打开Fragment

有没有办法做到这一点?

public class MainActivity extends AppCompatActivity {

    private TabLayout tabs;
    private NonSwipeViewPager viewpager;

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

        tabs = (TabLayout)findViewById(R.id.tabs);
        viewpager = (NonSwipeViewPager)findViewById(R.id.viewpager);
        viewpager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this));
        viewpager.setPagingEnabled(false);
        tabs.setupWithViewPager(viewpager);
        setupTabIcons();
    }

    public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
        final int PAGE_COUNT = 5;
        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) {
            switch (position) {
                case 0:
                    return new HomeFragment();
                case 1:
                    return new CategoryFragment();
                case 2:
                    return new AddProdFragment();
                case 3:
                    return new MyProdFragment();
                case 4:
                    return new MyProfileFragment();
                default:
                    return null;
            }
        }
    }

    private void setupTabIcons() {
        tabs.getTabAt(0).setIcon(R.drawable.home_white);
        tabs.getTabAt(1).setIcon(R.drawable.category_white);
        tabs.getTabAt(2).setIcon(R.drawable.upload_white);
        tabs.getTabAt(3).setIcon(R.drawable.listing_white);
        tabs.getTabAt(4).setIcon(R.drawable.user_white);
    }
}

2 个答案:

答案 0 :(得分:0)

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        public void onTabChanged(String tabId) {
            Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());

            switch (mTabHost.getCurrentTab()) {
            case 0:
                //do what you want when tab 0 is selected
                break;
            case 1:
                //do what you want when tab 1 is selected
                break;
            case 2:
                //do what you want when tab 2 is selected
                break;

            default:

                break;
            }
        }
    });

使用此选项卡单击监听器。

答案 1 :(得分:0)

建议不要在Tab开关上打开component restpath = "checkboxes" rest = true { remote void function checkboxes( string value restargsource = "query" default="" ) httpmethod = "GET" restpath = "" produces = "application/json" { var v = []; var params = getPageContext().getRequest().getParameterMap(); if ( StructKeyExists( params, "value" ) ) { v = params["value"]; } restSetResponse( { "status" = 200, "content" = SerializeJson( v ) } ); } } 。但无论如何,你可以做这样的事情。

Activity