【问题标题】:I have a Spinner inside navigation Drawer , when i select a spinner dropdown Item i am openning an activity and that activity have three TabLayout我在导航抽屉中有一个微调器,当我选择微调器下拉项时,我正在打开一个活动,该活动有三个 TabLayout
【发布时间】:2018-07-31 07:11:44
【问题描述】:

当我单击微调器的下拉菜单时,我想打开该活动的特定选项卡,我有三个使用 Fragment 的 Tablaout。 [在单击微调器上,我想打开一个活动,并且相关的 TabLayout 它可能是第二个或第三个 enter image description here

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

}

【问题讨论】:

  • 可能您可以通过Intent 传递标签索引(如果您使用的是固定标签)并在您的目标活动中使用getIntent() 并将该索引分配给TabLayout

标签: android android-viewpager fragment navigation-drawer android-tablayout


【解决方案1】:

试试这个,

在第一个活动中,

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();
}

【讨论】:

  • 它不工作它总是在第一个标签上
  • 你使用 tabLayout.getTabAt(pos).select();
  • viewPager.setCurrentItem(tab.getPosition());
  • 然后使用 viewPager.setCurrentItem(pos);
  • 这里的pos是什么
猜你喜欢
  • 2022-01-14
  • 2020-06-02
  • 1970-01-01
  • 2023-03-07
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多