首页 > 解决方案 > 我在导航抽屉内有一个微调器,当我选择一个微调器下拉项时,我正在打开一个活动,该活动有三个 TabLayout

问题描述

当我单击微调器的下拉菜单时,我想打开该活动的特定选项卡,我有三个使用 Fragment 的 Tablaout。[在点击微调器上,我想打开一个活动和相关的 TabLayout,它可能是第二个或第三个 在此处输入图像描述

   Spinner sp = (Spinner) navigationView.getMenu().findItem(R.id. shop).getActionView();
    sp.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,arraySpinner));
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


            switch(position){
                case 1:
                    strs = new Bundle();
                    strs.putInt("item", position);
                    strs.putString("Women", parent.getItemAtPosition(position).toString());
                    intent.putExtras(strs);
                    intent = new Intent(MainActivity.this, Shop.class);
                    startActivity(intent);
                    break;
                case 2:
                    intent = new Intent(MainActivity.this, Designer.class);
                    startActivity(intent);
                    break;

                case 3:
                    intent = new Intent(MainActivity.this, Designer.class);
                    startActivity(intent);
                    break;
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

另一个活动选项卡布局代码是:

   Integer item = data.getInt("item");
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
            if (tab.getPosition() == 1) {


               /* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
                        R.color.white));*/
                tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
                        R.color.white));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
                            R.color.white));
                }
            } else if (tab.getPosition() == 2) {
               /* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
                        android.R.color.white));*/
                tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
                        android.R.color.white));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
                            android.R.color.white));
                }
            } else {
               /* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
                        R.color.white));*/
                tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
                        R.color.white));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
                            R.color.white));
                }
            }
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

}

我用于替换浴缸布局的适配器类是:

public class PageAdapter extends FragmentPagerAdapter {

private int numOfTabs;

public PageAdapter(FragmentManager fm, int numOfTabs) {
    super(fm);
    this.numOfTabs = numOfTabs;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new WomenFragment();
        case 1:
            return new MenFragment();
        case 2:
            return new KidsFragment();
        default:
            return null;
    }
}

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

}

标签: androidandroid-viewpagerfragmentnavigation-drawerandroid-tablayout

解决方案


尝试这个,

在第一次活动中,

Spinner sp = (Spinner) navigationView.getMenu().findItem(R.id. shop).getActionView();
sp.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,arraySpinner));
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


        switch(position){
            case 1:
                Intent intent = new Intent(MainActivity.this, Shop.class);
                intent.putExtra("position",position)
                startActivity(intent);
                break;
            case 2:
                Intent intent = new Intent(MainActivity.this, Shop.class);
                intent.putExtra("position",position)
                startActivity(intent);
                break;

            case 3:
                Intent intent = new Intent(MainActivity.this, Shop.class);
                intent.putExtra("position",position)
                startActivity(intent);
                break;
        }
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

在另一个活动中,

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
        if (tab.getPosition() == 1) {


           /* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
                    R.color.white));*/
            tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
                    R.color.white));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
                        R.color.white));
            }
        } else if (tab.getPosition() == 2) {
           /* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
                    android.R.color.white));*/
            tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
                    android.R.color.white));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
                        android.R.color.white));
            }
        } else {
           /* toolbar.setBackgroundColor(ContextCompat.getColor(Shop.this,
                    R.color.white));*/
            tabLayout.setBackgroundColor(ContextCompat.getColor(Shop.this,
                    R.color.white));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(ContextCompat.getColor(Shop.this,
                        R.color.white));
            }
        }
    }
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

int pos = 0;
Intent intent = getIntent();
if(intent!=null){
    pos = intent.getStringExtra("position");
}
tabLayout.getTabAt(pos).select();
}

推荐阅读